Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds unit-tests for dnatracing methods #593

Merged
merged 5 commits into from
Jun 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ tests = [
"py",
"pytest",
"pytest-cov",
"pytest-lazy-fixture",
"pytest-mpl",
"pytest-regtest",
"filetype",
Expand Down
Binary file added tests/resources/dnatracing_image_circular.npy
Binary file not shown.
Binary file added tests/resources/dnatracing_image_linear.npy
Binary file not shown.
Binary file added tests/resources/dnatracing_mask_circular.npy
Binary file not shown.
Binary file added tests/resources/dnatracing_mask_linear.npy
Binary file not shown.
249 changes: 249 additions & 0 deletions tests/tracing/test_dnatracing_methods.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
"""Additional tests of dnaTracing methods"""
from pathlib import Path

import numpy as np
import pytest

from topostats.tracing.dnatracing import dnaTrace

# This is required because of the inheritance used throughout
# pylint: disable=redefined-outer-name
BASE_DIR = Path.cwd()
RESOURCES = BASE_DIR / "tests" / "resources"
PIXEL_SIZE = 0.4940029296875

LINEAR_IMAGE = np.load(RESOURCES / "dnatracing_image_linear.npy")
LINEAR_MASK = np.load(RESOURCES / "dnatracing_mask_linear.npy")
CIRCULAR_IMAGE = np.load(RESOURCES / "dnatracing_image_circular.npy")
CIRCULAR_MASK = np.load(RESOURCES / "dnatracing_mask_circular.npy")
MIN_SKELETON_SIZE = 10


@pytest.fixture
def dnatrace() -> dnaTrace:
"""Instantiated object of class dnaTrace for use in tests."""
_dnatrace = dnaTrace(
full_image_data=np.asarray([[1]]),
grains=None,
filename="test.spm",
pixel_size=PIXEL_SIZE,
min_skeleton_size=MIN_SKELETON_SIZE,
)
return _dnatrace


GRAINS = {}
GRAINS["vertical"] = np.asarray(
[
[0, 0, 0, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 0, 0, 0],
]
)
GRAINS["horizontal"] = np.asarray(
[
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
]
)
GRAINS["diagonal1"] = np.asarray(
[
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 1, 1, 1, 0, 0],
[0, 0, 0, 1, 1, 0, 0, 0, 0],
[0, 0, 1, 1, 0, 0, 0, 0, 0],
[0, 1, 1, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
]
)
GRAINS["diagonal2"] = np.asarray(
[
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0],
[0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0],
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, 0, 0, 0],
[0, 1, 1, 1, 1, 1, 0],
[0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
]
)
GRAINS["single_L"] = np.asarray(
[
[0, 0, 0, 0],
[0, 0, 1, 0],
[0, 0, 1, 0],
[0, 0, 1, 0],
[0, 1, 1, 0],
[0, 0, 0, 0],
]
)
GRAINS["double_L"] = np.asarray(
[
[0, 0, 0, 0, 0],
[0, 0, 0, 1, 0],
[0, 0, 1, 1, 0],
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0],
[0, 1, 1, 0, 0],
[0, 0, 0, 0, 0],
]
)

GRAINS["diagonal_end_single_L"] = np.asarray(
[
[0, 0, 0, 0, 0],
[0, 0, 0, 1, 0],
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0],
[0, 1, 1, 0, 0],
[0, 0, 0, 0, 0],
]
)
GRAINS["diagonal_end_straight"] = np.asarray(
[
[0, 0, 0, 0, 0],
[0, 0, 0, 1, 0],
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 0, 0, 0],
]
)
GRAINS["figure8"] = np.asarray(
[
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 1, 1, 1, 0],
[0, 0, 0, 0, 1, 0, 0, 1, 0],
[0, 0, 0, 0, 1, 0, 0, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 0, 0, 1, 0, 0, 0, 0],
[0, 1, 0, 0, 1, 0, 0, 0, 0],
[0, 1, 1, 1, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
]
)
GRAINS["three_ends"] = np.asarray(
[
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 1, 1, 1, 0],
[0, 0, 0, 0, 1, 0, 0, 1, 0],
[0, 0, 0, 0, 1, 0, 0, 1, 0],
[0, 1, 1, 1, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
]
)
GRAINS["six_ends"] = np.asarray(
[
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 1, 1, 1, 0],
[0, 0, 0, 0, 1, 0, 0, 1, 0],
[0, 0, 0, 0, 1, 0, 0, 1, 0],
[0, 1, 1, 1, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 1, 0],
[0, 0, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 0, 0, 1, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
]
)


@pytest.mark.parametrize(
"grain, num_linear, num_circular",
[
(GRAINS["vertical"], 1, 0),
(GRAINS["horizontal"], 1, 0),
(GRAINS["diagonal1"], 0, 1), # This is wrong, this IS a linear molecule
(GRAINS["diagonal2"], 1, 0),
(GRAINS["diagonal3"], 1, 0),
(GRAINS["circle"], 0, 1),
(GRAINS["blob"], 0, 1),
(GRAINS["cross"], 1, 0),
(GRAINS["single_L"], 1, 0),
(GRAINS["double_L"], 0, 1), # This is wrong, this IS a linear molecule
(GRAINS["diagonal_end_single_L"], 1, 0),
(GRAINS["diagonal_end_straight"], 1, 0),
(GRAINS["figure8"], 0, 1),
(GRAINS["three_ends"], 1, 0),
(GRAINS["six_ends"], 1, 0),
],
)
def test_linear_or_circular(dnatrace, grain: np.ndarray, num_linear: int, num_circular: int) -> None:
"""Test the linear_or_circular method with a range of different structures."""
linear_coordinates = {1: np.argwhere(grain == 1)}
dnatrace.linear_or_circular(linear_coordinates)
assert dnatrace.num_linear == num_linear
assert dnatrace.num_circular == num_circular
Loading