Skip to content

Commit

Permalink
Merge pull request #125 from paulsengroup/testing
Browse files Browse the repository at this point in the history
Improve tests
  • Loading branch information
robomics authored Nov 12, 2024
2 parents 285ffaa + cb2064a commit 6599f44
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 16 deletions.
5 changes: 3 additions & 2 deletions src/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ static nb::dict get_hic_attrs(const hictk::hic::File &hf) {

py_attrs["bin_size"] = hf.resolution();
py_attrs["format"] = "HIC";
py_attrs["format_version"] = hf.version();
py_attrs["format-version"] = hf.version();
py_attrs["assembly"] = hf.assembly();
py_attrs["format-url"] = "https://github.com/aidenlab/hic-format";
py_attrs["nbins"] = hf.bins().size();
Expand Down Expand Up @@ -301,7 +301,8 @@ void declare_file_class(nb::module_ &m) {

file.def("resolution", &hictk::File::resolution, "Get the bin size in bp.");
file.def("nbins", &hictk::File::nbins, "Get the total number of bins.");
file.def("nchroms", &hictk::File::nchroms, "Get the total number of chromosomes.");
file.def("nchroms", &hictk::File::nchroms, nb::arg("include_ALL") = false,
"Get the total number of chromosomes.");

file.def("attributes", &file::attributes, "Get file attributes as a dictionary.",
nb::rv_policy::take_ownership);
Expand Down
9 changes: 4 additions & 5 deletions test/test_bin_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,16 @@ def test_getters(self):
bins.get_id("abc", 100)

@pytest.mark.skipif(
not numpy_avail() or not pandas_avail() or not pyarrow_avail(),
reason="numpy, pandas, or pyarrow are not available",
not pandas_avail() or not pyarrow_avail(),
reason="pandas or pyarrow are not available",
)
def test_vectorized_getters(self):
import numpy as np

chroms = {"chr1": 1000, "chr2": 500}
bins = hictkpy.BinTable(chroms, 100)

assert len(bins.get(np.array([1, 1]))) == 2
assert len(bins.get_ids(np.array(["chr1", "chr1"]), np.array([1, 1]))) == 2
assert len(bins.get([1, 1])) == 2
assert len(bins.get_ids(["chr1", "chr1"], [1, 1])) == 2

@pytest.mark.skipif(not pandas_avail() or not pyarrow_avail(), reason="pandas is not available")
def test_merge(self):
Expand Down
2 changes: 1 addition & 1 deletion test/test_file_accessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class TestClass:
def test_attributes(self, file, resolution):
f = hictkpy.File(file, resolution)
assert f.resolution() == 100_000
# assert f.nchroms() == 8 # TODO enable after merging https://github.com/paulsengroup/hictk/pull/294
assert f.nchroms() == 8
assert f.nbins() == 1380

assert "chr2L" in f.chromosomes()
Expand Down
22 changes: 18 additions & 4 deletions test/test_file_creation_cool.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ def setup_method():
logging.basicConfig(level="INFO", force=True)
logging.getLogger().setLevel("INFO")

def test_accessors(self, file, resolution, tmpdir):
bins = hictkpy.File(file, resolution).bins()

path = tmpdir / "test.cool"
w = hictkpy.cooler.FileWriter(path, bins)

assert str(w).startswith("CoolFileWriter(")
assert w.path() == path
if resolution is None:
assert w.resolution() == 0
else:
assert w.resolution() == resolution
assert w.chromosomes() == bins.chromosomes()

def test_file_creation_thin_pixel(self, file, resolution, tmpdir):
f = hictkpy.File(file, resolution)
if f.bins().type() != "fixed":
Expand All @@ -39,7 +53,7 @@ def test_file_creation_thin_pixel(self, file, resolution, tmpdir):
df = f.fetch(join=False).to_df()
expected_sum = df["count"].sum()

path = tmpdir / "test1.cool"
path = tmpdir / "test.cool"
w = hictkpy.cooler.FileWriter(path, f.chromosomes(), f.resolution())

chunk_size = 1000
Expand Down Expand Up @@ -67,7 +81,7 @@ def test_file_creation(self, file, resolution, tmpdir):
df = f.fetch(join=True).to_df()
expected_sum = df["count"].sum()

path = tmpdir / "test2.cool"
path = tmpdir / "test.cool"
w = hictkpy.cooler.FileWriter(path, f.chromosomes(), f.resolution())

chunk_size = 1000
Expand All @@ -93,7 +107,7 @@ def test_file_creation_bin_table(self, file, resolution, tmpdir):
df = f.fetch(join=True).to_df()
expected_sum = df["count"].sum()

path = tmpdir / "test2.cool"
path = tmpdir / "test.cool"
w = hictkpy.cooler.FileWriter(path, f.bins())

chunk_size = 1000
Expand Down Expand Up @@ -122,7 +136,7 @@ def test_file_creation_float_counts(self, file, resolution, tmpdir):
df["count"] += 0.12345
expected_sum = df["count"].sum()

path = tmpdir / "test3.cool"
path = tmpdir / "test.cool"
w = hictkpy.cooler.FileWriter(path, f.chromosomes(), f.resolution())

chunk_size = 1000
Expand Down
32 changes: 29 additions & 3 deletions test/test_file_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

import pathlib

import pytest

import hictkpy

testdir = pathlib.Path(__file__).resolve().parent
Expand All @@ -17,7 +15,7 @@


class TestClass:
def test_validators(self):
def test_valid_formats(self):
assert hictkpy.is_cooler(cool_file)
assert not hictkpy.is_cooler(hic_file)

Expand All @@ -26,3 +24,31 @@ def test_validators(self):

assert hictkpy.is_scool_file(scool_file)
assert not hictkpy.is_scool_file(cool_file)

assert hictkpy.is_hic(hic_file)
assert not hictkpy.is_hic(cool_file)

def test_invalid_formats(self):
path = pathlib.Path(__file__).resolve()

assert not hictkpy.is_cooler(path)
assert not hictkpy.is_mcool_file(path)
assert not hictkpy.is_scool_file(path)
assert not hictkpy.is_hic(path)

def test_invalid_files(self):
non_existing_file = testdir / "foobar.123"
assert not non_existing_file.exists()

assert not hictkpy.is_cooler(non_existing_file)
assert not hictkpy.is_mcool_file(non_existing_file)
assert not hictkpy.is_scool_file(non_existing_file)
assert not hictkpy.is_hic(non_existing_file)

folder = testdir
assert folder.is_dir()

assert not hictkpy.is_cooler(folder)
assert not hictkpy.is_mcool_file(folder)
assert not hictkpy.is_scool_file(folder)
assert not hictkpy.is_hic(folder)
8 changes: 7 additions & 1 deletion test/test_singlecell_file_accessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,19 @@


class TestClass:
def test_attributes(self, file):
def test_accessors(self, file):
f = hictkpy.cooler.SingleCellFile(file)

assert str(f).startswith("SingleCellFile(")

assert f.path() == file
assert f.resolution() == 100_000
assert len(f.chromosomes()) == 20
assert len(f.bins()) == 26398
assert len(f.cells()) == 5

assert f.attributes()["format"] == "HDF5::SCOOL"
assert f["GSM2687248_41669_ACAGTG-R1-DpnII.100000.cool"].resolution() == 100_000

with pytest.raises(Exception):
f["ABC"] # noqa

0 comments on commit 6599f44

Please sign in to comment.