Skip to content

Commit

Permalink
[TEST] restructure tests
Browse files Browse the repository at this point in the history
test for functions from a specific python file are grouped together in the same structure under unit tests, larger workflow tests are add in e2e testing
  • Loading branch information
sophiamaedler committed Feb 11, 2025
1 parent c4b8df7 commit 6b1e773
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 225 deletions.
68 changes: 0 additions & 68 deletions tests/e2e_tests/segmentation_workflow.py

This file was deleted.

File renamed without changes.
54 changes: 54 additions & 0 deletions tests/unit_tests/pipeline/test_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#######################################################
# Unit tests for ../pipeline/base.py
#######################################################

import os
import tempfile

from scportrait.pipeline._base import Logable, ProcessingStep


def test_logable_init():
with tempfile.TemporaryDirectory() as temp_dir:
logable = Logable(directory=temp_dir, debug=False)
assert not logable.debug


def test_logable_log():
with tempfile.TemporaryDirectory() as temp_dir:
logable = Logable(directory=temp_dir, debug=True)
logable.log("Testing")

log_path = os.path.join(temp_dir, logable.DEFAULT_LOG_NAME)
assert os.path.isfile(log_path)

with open(log_path) as f:
log_content = f.read()
assert "Testing" in log_content


def test_processing_step_init():
config = {"setting1": "value1"}
with tempfile.TemporaryDirectory() as temp_dir:
processing_step = ProcessingStep(config, f"{temp_dir}/test_step", temp_dir, debug=True)

assert processing_step.debug
assert config == processing_step.config


def test_processing_step_register_parameter():
config = {"setting1": "value1"}
with tempfile.TemporaryDirectory() as temp_dir:
processing_step = ProcessingStep(config, f"{temp_dir}/test_step", temp_dir)

# Test registering a new parameter
processing_step.register_parameter("setting2", "value2")
assert "setting2" in processing_step.config
assert "value2" == processing_step.config["setting2"]


def test_processing_step_get_directory():
config = {"setting1": "value1"}
with tempfile.TemporaryDirectory() as temp_dir:
processing_step = ProcessingStep(config, f"{temp_dir}/test_step", temp_dir)
assert f"{temp_dir}/test_step" == processing_step.get_directory()
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from skimage import data # test datasets for segmentation testing

#######################################################
# Unit tests for ../proccessing/segmentation.py
# Unit tests for scportrait.pipeline._utils.segmentation
#######################################################
from scportrait.pipeline._utils.segmentation import (
_segment_threshold,
Expand Down Expand Up @@ -213,159 +213,3 @@ def test_numba_mask_centroid():
assert np.allclose(centers, expected_centers, atol=1e-6)
assert np.all(points_class == expected_points_class)
assert np.all(ids == expected_ids)


#######################################################
# Unit tests for ../proccessing/preprocessing.py
#######################################################

from scportrait.processing.images._image_processing import (
MinMax,
_percentile_norm,
percentile_normalization,
rolling_window_mean,
)


def test_percentile_norm():
rng = np.random.default_rng()
img = rng.random((4, 4))
norm_img = _percentile_norm(img, 0.1, 0.9)
assert np.min(norm_img) == pytest.approx(0)
assert np.max(norm_img) == pytest.approx(1)


def test_percentile_normalization_C_H_W():
rng = np.random.default_rng()
test_array = rng.integers(2, size=(3, 100, 100))
test_array[:, 10:11, 10:11] = -1
test_array[:, 12:13, 12:13] = 3

normalized = percentile_normalization(test_array, 0.05, 0.95)
assert np.max(normalized) == pytest.approx(1)
assert np.min(normalized) == pytest.approx(0)


def test_percentile_normalization_H_W():
rng = np.random.default_rng()
test_array = rng.integers(2, size=(100, 100))
test_array[10:11, 10:11] = -1
test_array[12:13, 12:13] = 3

normalized = percentile_normalization(test_array, 0.05, 0.95)
assert np.max(normalized) == pytest.approx(1)
assert np.min(normalized) == pytest.approx(0)


def test_rolling_window_mean():
rng = np.random.default_rng()
array = rng.random((10, 10))
rolling_array = rolling_window_mean(array, size=5, scaling=False)
assert np.all(array.shape == rolling_array.shape)


def test_MinMax():
rng = np.random.default_rng()
array = rng.random((10, 10))
normalized_array = MinMax(array)
assert np.min(normalized_array) == 0
assert np.max(normalized_array) == 1


#######################################################
# Unit tests for ../proccessing/utils.py
#######################################################

from scportrait.pipeline._utils.helper import flatten
from scportrait.plotting.vis import plot_image, visualize_class


def test_flatten():
nested_list = [[1, 2, 3], [4, 5], [6, 7, 8, 9]]
expected_output = [1, 2, 3, 4, 5, 6, 7, 8, 9]
assert flatten(nested_list) == expected_output


def test_visualize_class():
class_ids = [1, 2]
seg_map = np.array([[0, 1, 0], [1, 2, 1], [2, 0, 1]])
rng = np.random.default_rng()
background = rng.random((3, 3)) * 255
# Since this function does not return anything, we just check if it produces any exceptions
try:
visualize_class(class_ids, seg_map, background)
except (ValueError, TypeError) as e:
pytest.fail(f"visualize_class raised exception: {str(e)}")


def test_plot_image(tmpdir):
rng = np.random.default_rng()
array = rng.random((10, 10))
save_name = tmpdir.join("test_plot_image")

# Since this function does not return anything, we just check if it produces any exceptions
try:
plot_image(array, size=(5, 5), save_name=save_name)
except (OSError, ValueError, TypeError) as e:
pytest.fail(f"plot_image raised exception: {str(e)}")
assert os.path.isfile(str(save_name) + ".png")


#######################################################
# Unit tests for ../pipeline/base.py
#######################################################

import tempfile

from scportrait.pipeline._base import Logable, ProcessingStep


def test_logable_init():
with tempfile.TemporaryDirectory() as temp_dir:
logable = Logable(directory=temp_dir, debug=False)
assert not logable.debug


def test_logable_log():
with tempfile.TemporaryDirectory() as temp_dir:
logable = Logable(directory=temp_dir, debug=True)
logable.log("Testing")

log_path = os.path.join(temp_dir, logable.DEFAULT_LOG_NAME)
assert os.path.isfile(log_path)

with open(log_path) as f:
log_content = f.read()
assert "Testing" in log_content


def test_processing_step_init():
config = {"setting1": "value1"}
with tempfile.TemporaryDirectory() as temp_dir:
processing_step = ProcessingStep(config, f"{temp_dir}/test_step", temp_dir, debug=True)

assert processing_step.debug
assert config == processing_step.config


def test_processing_step_register_parameter():
config = {"setting1": "value1"}
with tempfile.TemporaryDirectory() as temp_dir:
processing_step = ProcessingStep(config, f"{temp_dir}/test_step", temp_dir)

# Test registering a new parameter
processing_step.register_parameter("setting2", "value2")
assert "setting2" in processing_step.config
assert "value2" == processing_step.config["setting2"]


def test_processing_step_get_directory():
config = {"setting1": "value1"}
with tempfile.TemporaryDirectory() as temp_dir:
processing_step = ProcessingStep(config, f"{temp_dir}/test_step", temp_dir)
assert f"{temp_dir}/test_step" == processing_step.get_directory()


# general test to check that testing is working
def test_test():
assert 1 == 1
11 changes: 11 additions & 0 deletions tests/unit_tests/pipeline/test_utils_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#######################################################
# Unit tests for ../pipeline/_utils/helper.py
#######################################################

from scportrait.pipeline._utils.helper import flatten


def test_flatten():
nested_list = [[1, 2, 3], [4, 5], [6, 7, 8, 9]]
expected_output = [1, 2, 3, 4, 5, 6, 7, 8, 9]
assert flatten(nested_list) == expected_output
34 changes: 34 additions & 0 deletions tests/unit_tests/plotting/test_plotting_vis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#######################################################
# Unit tests for ../plotting/vis.py
#######################################################
import os

import numpy as np
import pytest

from scportrait.plotting.vis import plot_image, visualize_class


def test_visualize_class():
class_ids = [1, 2]
seg_map = np.array([[0, 1, 0], [1, 2, 1], [2, 0, 1]])
rng = np.random.default_rng()
background = rng.random((3, 3)) * 255
# Since this function does not return anything, we just check if it produces any exceptions
try:
visualize_class(class_ids, seg_map, background)
except (ValueError, TypeError) as e:
pytest.fail(f"visualize_class raised exception: {str(e)}")


def test_plot_image(tmpdir):
rng = np.random.default_rng()
array = rng.random((10, 10))
save_name = tmpdir.join("test_plot_image")

# Since this function does not return anything, we just check if it produces any exceptions
try:
plot_image(array, size=(5, 5), save_name=save_name)
except (OSError, ValueError, TypeError) as e:
pytest.fail(f"plot_image raised exception: {str(e)}")
assert os.path.isfile(str(save_name) + ".png")
Loading

0 comments on commit 6b1e773

Please sign in to comment.