Skip to content

Commit

Permalink
tests: Remove restriction on pytest<8.0.0
Browse files Browse the repository at this point in the history
Closes #915

Switches to using `request.getfixturevalue()` and removes dependency on `pytest-lazy-fixture`.

Other things addressed...

+ Adds `pytest.param()` to each test in `tests/tracing/test_dnatracing_single_grain.py` along with `id` argument.
+ Orders dependencies alphabetically in `pyproject.toml` for ease of reading/searching.
  • Loading branch information
ns-rse committed Sep 24, 2024
1 parent e9fc3ba commit cecf60f
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 44 deletions.
5 changes: 2 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ dependencies = [
"AFMReader",
"h5py",
"igor2",
"keras",
"matplotlib",
"numpy",
"numpyencoder",
Expand All @@ -54,16 +55,14 @@ dependencies = [
"snoop",
"tifffile",
"tqdm",
"keras",
"tensorflow",
]

[project.optional-dependencies]
tests = [
"pytest<8.0.0",
"pytest",
"pytest-cov",
"pytest-github-actions-annotate-failures",
"pytest-lazy-fixture",
"pytest-mpl",
"pytest-regtest",
"filetype",
Expand Down
101 changes: 60 additions & 41 deletions tests/tracing/test_dnatracing_single_grain.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import numpy as np
import pytest
from pytest_lazyfixture import lazy_fixture

from topostats.tracing.dnatracing import (
crop_array,
Expand Down Expand Up @@ -57,33 +56,41 @@ def dnatrace_circular() -> dnaTrace:
@pytest.mark.parametrize(
("dnatrace", "gauss_image_sum"),
[
(lazy_fixture("dnatrace_linear"), 5.517763534147536e-06),
(lazy_fixture("dnatrace_circular"), 6.126947266262167e-06),
pytest.param("dnatrace_linear", 5.517763534147536e-06, id="linear"),
pytest.param("dnatrace_circular", 6.126947266262167e-06, id="circular"),
],
)
def test_gaussian_filter(dnatrace: dnaTrace, gauss_image_sum: float) -> None:
def test_gaussian_filter(dnatrace: dnaTrace, gauss_image_sum: float, request) -> None:
"""Test of the method."""
dnatrace = request.getfixturevalue(dnatrace)
dnatrace.gaussian_filter()
assert dnatrace.gauss_image.sum() == pytest.approx(gauss_image_sum)


@pytest.mark.parametrize(
("dnatrace", "skeletonisation_method", "length", "start", "end"),
[
(lazy_fixture("dnatrace_linear"), "topostats", 120, np.asarray([28, 47]), np.asarray([106, 87])),
(lazy_fixture("dnatrace_circular"), "topostats", 150, np.asarray([59, 59]), np.asarray([113, 54])),
(lazy_fixture("dnatrace_linear"), "zhang", 170, np.asarray([28, 47]), np.asarray([106, 87])),
(lazy_fixture("dnatrace_circular"), "zhang", 184, np.asarray([43, 95]), np.asarray([113, 54])),
(lazy_fixture("dnatrace_linear"), "lee", 130, np.asarray([27, 45]), np.asarray([106, 87])),
(lazy_fixture("dnatrace_circular"), "lee", 177, np.asarray([45, 93]), np.asarray([114, 53])),
(lazy_fixture("dnatrace_linear"), "thin", 187, np.asarray([27, 45]), np.asarray([106, 83])),
(lazy_fixture("dnatrace_circular"), "thin", 190, np.asarray([38, 85]), np.asarray([115, 52])),
pytest.param(
"dnatrace_linear", "topostats", 120, np.asarray([28, 47]), np.asarray([106, 87]), id="linear topostats"
),
pytest.param(
"dnatrace_circular", "topostats", 150, np.asarray([59, 59]), np.asarray([113, 54]), id="circular topostats"
),
pytest.param("dnatrace_linear", "zhang", 170, np.asarray([28, 47]), np.asarray([106, 87]), id="linear zhang"),
pytest.param(
"dnatrace_circular", "zhang", 184, np.asarray([43, 95]), np.asarray([113, 54]), id="circular zhang"
),
pytest.param("dnatrace_linear", "lee", 130, np.asarray([27, 45]), np.asarray([106, 87]), id="linear lee"),
pytest.param("dnatrace_circular", "lee", 177, np.asarray([45, 93]), np.asarray([114, 53]), id="circular lee"),
pytest.param("dnatrace_linear", "thin", 187, np.asarray([27, 45]), np.asarray([106, 83]), id="linear thin"),
pytest.param("dnatrace_circular", "thin", 190, np.asarray([38, 85]), np.asarray([115, 52]), id="circular thin"),
],
)
def test_get_disordered_trace(
dnatrace: dnaTrace, skeletonisation_method: str, length: int, start: tuple, end: tuple
def test_get_disordered_trace( # pylint: disable=too-many-positional-arguments
dnatrace: dnaTrace, skeletonisation_method: str, length: int, start: tuple, end: tuple, request
) -> None:
"""Test of get_disordered_trace the method."""
dnatrace = request.getfixturevalue(dnatrace)
dnatrace.skeletonisation_method = skeletonisation_method
dnatrace.gaussian_filter()
dnatrace.get_disordered_trace()
Expand All @@ -103,12 +110,13 @@ def test_get_disordered_trace(
@pytest.mark.parametrize(
("dnatrace", "mol_is_circular"),
[
# (lazy_fixture("dnatrace_linear"), False),
(lazy_fixture("dnatrace_circular"), True),
# pytest.param("dnatrace_linear"), False, id="linear"),
pytest.param("dnatrace_circular", True, id="circular"),
],
)
def test_linear_or_circular(dnatrace: dnaTrace, mol_is_circular: int) -> None:
def test_linear_or_circular(dnatrace: dnaTrace, mol_is_circular: int, request) -> None:
"""Test of the linear_or_circular method."""
dnatrace = request.getfixturevalue(dnatrace)
dnatrace.min_skeleton_size = MIN_SKELETON_SIZE
dnatrace.gaussian_filter()
dnatrace.get_disordered_trace()
Expand All @@ -119,17 +127,18 @@ def test_linear_or_circular(dnatrace: dnaTrace, mol_is_circular: int) -> None:
@pytest.mark.parametrize(
("dnatrace", "length", "start", "end"),
[
(lazy_fixture("dnatrace_linear"), 118, np.asarray([28, 48]), np.asarray([88, 70])),
(lazy_fixture("dnatrace_circular"), 151, np.asarray([59, 59]), np.asarray([59, 59])),
pytest.param("dnatrace_linear", 118, np.asarray([28, 48]), np.asarray([88, 70]), id="linear"),
pytest.param("dnatrace_circular", 151, np.asarray([59, 59]), np.asarray([59, 59]), id="circular"),
],
)
def test_get_ordered_traces(dnatrace: dnaTrace, length: int, start: np.array, end: np.array) -> None:
def test_get_ordered_traces(dnatrace: dnaTrace, length: int, start: np.array, end: np.array, request) -> None:
"""Test of the get_ordered_traces method.
Note the coordinates at the start and end differ from the fixtures for test_get_disordered_trace, but that the
circular molecule starts and ends in the same place but the linear doesn't (even though it is currently reported as
being circular!).
"""
dnatrace = request.getfixturevalue(dnatrace)
dnatrace.gaussian_filter()
dnatrace.get_disordered_trace()
dnatrace.linear_or_circular(dnatrace.disordered_trace)
Expand All @@ -149,12 +158,13 @@ def test_get_ordered_traces(dnatrace: dnaTrace, length: int, start: np.array, en
@pytest.mark.parametrize(
("dnatrace", "length", "start", "end"),
[
(lazy_fixture("dnatrace_linear"), 118, 8.8224769e-10, 1.7610771e-09),
(lazy_fixture("dnatrace_circular"), 151, 2.5852866e-09, 2.5852866e-09),
pytest.param("dnatrace_linear", 118, 8.8224769e-10, 1.7610771e-09, id="linear"),
pytest.param("dnatrace_circular", 151, 2.5852866e-09, 2.5852866e-09, id="circular"),
],
)
def test_get_ordered_trace_heights(dnatrace: dnaTrace, length: int, start: float, end: float) -> None:
def test_get_ordered_trace_heights(dnatrace: dnaTrace, length: int, start: float, end: float, request) -> None:
"""Test of the get_trace_heights method."""
dnatrace = request.getfixturevalue(dnatrace)
dnatrace.gaussian_filter()
dnatrace.get_disordered_trace()
dnatrace.linear_or_circular(dnatrace.disordered_trace)
Expand All @@ -169,12 +179,15 @@ def test_get_ordered_trace_heights(dnatrace: dnaTrace, length: int, start: float
@pytest.mark.parametrize(
("dnatrace", "length", "start", "end"),
[
pytest.param(lazy_fixture("dnatrace_linear"), 118, 0.0, 6.8234101e-08, id="linear"),
pytest.param(lazy_fixture("dnatrace_circular"), 151, 0.0, 8.3513084e-08, id="circular"),
pytest.param("dnatrace_linear", 118, 0.0, 6.8234101e-08, id="linear"),
pytest.param("dnatrace_circular", 151, 0.0, 8.3513084e-08, id="circular"),
],
)
def test_ordered_get_trace_cumulative_distances(dnatrace: dnaTrace, length: int, start: float, end: float) -> None:
def test_ordered_get_trace_cumulative_distances(
dnatrace: dnaTrace, length: int, start: float, end: float, request
) -> None:
"""Test of the get_trace_cumulative_distances method."""
dnatrace = request.getfixturevalue(dnatrace)
dnatrace.gaussian_filter()
dnatrace.get_disordered_trace()
dnatrace.linear_or_circular(dnatrace.disordered_trace)
Expand Down Expand Up @@ -215,12 +228,13 @@ def test_coord_dist(coordinate_list: list, pixel_to_nm_scaling: float, target_li
@pytest.mark.parametrize(
("dnatrace", "length", "start", "end"),
[
(lazy_fixture("dnatrace_linear"), 118, np.asarray([28, 49]), np.asarray([88, 75])),
(lazy_fixture("dnatrace_circular"), 151, np.asarray([58, 58]), np.asarray([58, 58])),
pytest.param("dnatrace_linear", 118, np.asarray([28, 49]), np.asarray([88, 75]), id="linear"),
pytest.param("dnatrace_circular", 151, np.asarray([58, 58]), np.asarray([58, 58]), id="circular"),
],
)
def test_get_fitted_traces(dnatrace: dnaTrace, length: int, start: np.array, end: np.array) -> None:
def test_get_fitted_traces(dnatrace: dnaTrace, length: int, start: np.array, end: np.array, request) -> None:
"""Test of the method get_fitted_traces()."""
dnatrace = request.getfixturevalue(dnatrace)
dnatrace.gaussian_filter()
dnatrace.get_disordered_trace()
dnatrace.linear_or_circular(dnatrace.disordered_trace)
Expand All @@ -242,22 +256,25 @@ def test_get_fitted_traces(dnatrace: dnaTrace, length: int, start: np.array, end
@pytest.mark.parametrize(
("dnatrace", "length", "start", "end"),
[
(
lazy_fixture("dnatrace_linear"),
pytest.param(
"dnatrace_linear",
1652,
np.asarray([35.357143, 46.714286]),
np.asarray([35.357143, 46.714286]),
id="linear",
),
(
lazy_fixture("dnatrace_circular"),
pytest.param(
"dnatrace_circular",
2114,
np.asarray([59.285714, 65.428571]),
np.asarray([59.285714, 65.428571]),
id="circular",
),
],
)
def test_get_splined_traces(dnatrace: dnaTrace, length: int, start: np.array, end: np.array) -> None:
def test_get_splined_traces(dnatrace: dnaTrace, length: int, start: np.array, end: np.array, request) -> None:
"""Test of the method for get_splined_traces()."""
dnatrace = request.getfixturevalue(dnatrace)
dnatrace.gaussian_filter()
dnatrace.get_disordered_trace()
dnatrace.linear_or_circular(dnatrace.disordered_trace)
Expand All @@ -280,12 +297,13 @@ def test_get_splined_traces(dnatrace: dnaTrace, length: int, start: np.array, en
@pytest.mark.parametrize(
("dnatrace", "contour_length"),
[
(lazy_fixture("dnatrace_linear"), 9.040267985905398e-08),
(lazy_fixture("dnatrace_circular"), 7.617314045334366e-08),
pytest.param("dnatrace_linear", 9.040267985905398e-08, id="linear"),
pytest.param("dnatrace_circular", 7.617314045334366e-08, id="circular"),
],
)
def test_measure_contour_length(dnatrace: dnaTrace, contour_length: float) -> None:
def test_measure_contour_length(dnatrace: dnaTrace, contour_length: float, request) -> None:
"""Test of the method measure_contour_length()."""
dnatrace = request.getfixturevalue(dnatrace)
dnatrace.gaussian_filter()
dnatrace.get_disordered_trace()
dnatrace.linear_or_circular(dnatrace.disordered_trace)
Expand All @@ -301,12 +319,13 @@ def test_measure_contour_length(dnatrace: dnaTrace, contour_length: float) -> No
@pytest.mark.parametrize(
("dnatrace", "end_to_end_distance"),
[
(lazy_fixture("dnatrace_linear"), 0),
(lazy_fixture("dnatrace_circular"), 0),
pytest.param("dnatrace_linear", 0, id="linear"),
pytest.param("dnatrace_circular", 0, id="circular"),
],
)
def test_measure_end_to_end_distance(dnatrace: dnaTrace, end_to_end_distance: float) -> None:
def test_measure_end_to_end_distance(dnatrace: dnaTrace, end_to_end_distance: float, request) -> None:
"""Test of the method measure_end_to_end_distance()."""
dnatrace = request.getfixturevalue(dnatrace)
dnatrace.gaussian_filter()
dnatrace.get_disordered_trace()
dnatrace.linear_or_circular(dnatrace.disordered_trace)
Expand Down Expand Up @@ -549,7 +568,7 @@ def test_grain_anchor(array_shape: tuple, bounding_box: list, pad_width: int, ta
),
],
)
def test_trace_grain(
def test_trace_grain( # pylint: disable=too-many-positional-arguments
cropped_image: np.ndarray,
cropped_mask: np.ndarray,
filename: str,
Expand Down

0 comments on commit cecf60f

Please sign in to comment.