Skip to content

Commit

Permalink
added tests for buffered reader
Browse files Browse the repository at this point in the history
  • Loading branch information
Ewan Barr committed Apr 2, 2024
1 parent a79cc17 commit ade309b
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
12 changes: 11 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ def tmpfile(tmp_path_factory, content=""):
fn.write_text(content)
return fn.as_posix()


@pytest.fixture(scope="session", autouse=True)
def filfile_1bit():
return Path(_datadir / "parkes_1bit.fil").as_posix()
Expand All @@ -37,6 +36,17 @@ def filfile_8bit_1():
def filfile_8bit_2():
return Path(_datadir / "parkes_8bit_2.fil").as_posix()

@pytest.fixture(scope="session", autouse=True)
def filterbank_files():
return [
Path(_datadir / "parkes_1bit.fil").as_posix(),
Path(_datadir / "parkes_2bit.fil").as_posix(),
Path(_datadir / "parkes_4bit.fil").as_posix(),
[Path(_datadir / "parkes_8bit_1.fil").as_posix(),
Path(_datadir / "parkes_8bit_2.fil").as_posix()],
Path(_datadir / "tutorial.fil").as_posix(),
Path(_datadir / "tutorial_2bit.fil").as_posix(),
]

@pytest.fixture(scope="session", autouse=True)
def fitsfile_4bit():
Expand Down
55 changes: 55 additions & 0 deletions tests/test_readers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pytest
import numpy as np
from sigpyproc.readers import FilReader, PFITSReader, PulseExtractor
from sigpyproc.header import Header
Expand Down Expand Up @@ -51,6 +52,60 @@ def test_read_dedisp_block_outofrange_dm(self, filfile_8bit_1):
fil = FilReader(filfile_8bit_1)
with np.testing.assert_raises(ValueError):
fil.read_dedisp_block(0, 100, 10000)

def test_read_plan(self, filfile_8bit_1):
fil = FilReader(filfile_8bit_1)
for nsamps, ii, data in fil.read_plan(gulp=512, nsamps=1024):
assert isinstance(data, np.ndarray)

def test_read_plan(self, filterbank_files):
for filfile in filterbank_files:
fil = FilReader(filfile)
for nsamps, ii, data in fil.read_plan(gulp=512, nsamps=1024):
assert isinstance(data, np.ndarray)

def test_read_plan_buffered(self, filterbank_files):
for filfile in filterbank_files:
fil = FilReader(filfile)
for nsamps, ii, data in fil.read_plan_buffered(gulp=512, nsamps=1024):
assert isinstance(data, np.ndarray)

def test_compare_buffered_and_nonbuffered(self, filterbank_files):
def allocator(nbytes):
buffer = np.zeros(nbytes, dtype="ubyte")
return memoryview(buffer)

for filfile in filterbank_files:
fil0 = FilReader(filfile)
plan0 = fil0.read_plan(quiet=True)
fil1 = FilReader(filfile)
plan1 = fil1.read_plan_buffered(quiet=True, allocator=allocator)
for (nsamps0, ii0, data0), (nsamps1, ii1, data1) in zip(plan0, plan1):
assert nsamps0 == nsamps1
assert ii0 == ii1
assert np.array_equal(data0, data1)

def test_nsamps_eq_skipback(self, filfile_8bit_1):
fil = FilReader(filfile_8bit_1)
with pytest.raises(ValueError):
for nsamps, ii, data in fil.read_plan_buffered(gulp=512, nsamps=1024, skipback=1024):
pass

def test_read_plan_custom_allocator(self, filfile_8bit_1):
fil = FilReader(filfile_8bit_1)
def allocator(nbytes):
buffer = np.zeros(nbytes, dtype="ubyte")
return memoryview(buffer)
for nsamps, ii, data in fil.read_plan_buffered(allocator=allocator):
pass

def test_read_plan_invalid_custom_allocator(self, filfile_8bit_1):
fil = FilReader(filfile_8bit_1)
def allocator():
pass
with pytest.raises(TypeError):
for nsamps, ii, data in fil.read_plan_buffered(allocator=allocator):
pass


class TestPFITSReader(object):
Expand Down

0 comments on commit ade309b

Please sign in to comment.