Skip to content

Commit

Permalink
Back to using warnings so we can test it properly
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobnissen committed Nov 6, 2023
1 parent 1af1ba5 commit 344c47e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
35 changes: 30 additions & 5 deletions test/test_parsecontigs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
import unittest
import random
import numpy as np
import warnings

import testtools
from vamb.parsecontigs import Composition, CompositionMetaData


class TestReadContigs(unittest.TestCase):
records = []
large_io = io.BytesIO()
io = io.BytesIO()

@classmethod
Expand All @@ -21,8 +23,14 @@ def setUpClass(cls):
cls.io.write(i.format().encode())
cls.io.write(b"\n")

for i in range(25_000):
record = testtools.make_randseq(rng, 250, 300)
cls.large_io.write(record.format().encode())
cls.large_io.write(b"\n")

def setUp(self):
self.io.seek(0)
self.large_io.seek(0)

def test_unique_names(self):
with self.assertRaises(ValueError):
Expand All @@ -33,9 +41,22 @@ def test_unique_names(self):
1000,
)

# Does not warn
def test_nowarn(self):
with warnings.catch_warnings():
warnings.simplefilter("error", UserWarning)
Composition.from_file(self.large_io, minlength=250)

def test_warns_n_contigs(self):
with self.assertWarns(UserWarning):
Composition.from_file(self.io, minlength=250)

def test_filter_minlength(self):
minlen = 500
composition = Composition.from_file(self.io, minlength=450)

with self.assertWarns(UserWarning):
composition = Composition.from_file(self.io, minlength=450)

md = composition.metadata
hash1 = md.refhash

Expand Down Expand Up @@ -74,7 +95,8 @@ def test_minlength(self):
Composition.from_file(self.io, minlength=3)

def test_properties(self):
composition = Composition.from_file(self.io, minlength=420)
with self.assertWarns(UserWarning):
composition = Composition.from_file(self.io, minlength=420)
passed = list(filter(lambda x: len(x.sequence) >= 420, self.records))

self.assertEqual(composition.nseqs, len(composition.metadata.identifiers))
Expand All @@ -96,7 +118,8 @@ def test_properties(self):

def test_save_load(self):
buf = io.BytesIO()
composition_1 = Composition.from_file(self.io)
with self.assertWarns(UserWarning):
composition_1 = Composition.from_file(self.io)
md1 = composition_1.metadata
composition_1.save(buf)
buf.seek(0)
Expand Down Expand Up @@ -126,8 +149,10 @@ def test_windows_newlines(self):

buf1.seek(0)
buf2.seek(0)
comp1 = Composition.from_file(buf1)
comp2 = Composition.from_file(buf2)
with self.assertWarns(UserWarning):
comp1 = Composition.from_file(buf1)
with self.assertWarns(UserWarning):
comp2 = Composition.from_file(buf2)

self.assertEqual(comp1.metadata.refhash, comp2.metadata.refhash)
self.assertTrue(np.all(comp1.matrix == comp2.matrix))
3 changes: 2 additions & 1 deletion vamb/parsecontigs.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from collections.abc import Iterable, Sequence
from typing import IO, Union, TypeVar, Optional, IO
from pathlib import Path
import warnings

# This kernel is created in src/create_kernel.py. See that file for explanation
_KERNEL: _np.ndarray = _vambtools.read_npz(
Expand Down Expand Up @@ -221,7 +222,7 @@ def from_file(
"You may want to bin more samples as a time, lower the beta parameter, "
"or use a different binner altogether."
)
print(message, file=sys.stderr)
warnings.warn(message)
if logfile is not None:
print("\n", file=logfile)
print(message, file=logfile)
Expand Down
12 changes: 12 additions & 0 deletions vamb/vambtools.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@
from collections.abc import Iterable, Iterator, Generator
from typing import Optional, IO, Union
from pathlib import Path
import warnings


def showwarning_override(message, category, filename, lineno, file=None, line=None):
print(str(message) + "\n", file=file)


# It may seem horrifying to override a stdlib method, but this is the way recommended by the
# warnings documentation.
# We do it because it's the only way I know to prevent displaying file numbers and source
# code to our users, which I think is a terrible user experience
warnings.showwarning = showwarning_override


class PushArray:
Expand Down

0 comments on commit 344c47e

Please sign in to comment.