diff --git a/brainglobe_utils/cells/cells.py b/brainglobe_utils/cells/cells.py index c420538..8537b32 100644 --- a/brainglobe_utils/cells/cells.py +++ b/brainglobe_utils/cells/cells.py @@ -252,9 +252,16 @@ def group_cells_by_z(cells: List[Cell]) -> DefaultDict[float, List[Cell]]: """ For a list of Cells return a dict of lists of cells, grouped by plane. - :param list cells: list of cells from cellfinder.cells.cells.Cell - :return: defaultdict, with each key being a plane (e.g. 1280) - and each entry being a list of Cells + Parameters + ---------- + cells : List of Cell + List of cells from cellfinder.cells.cells.Cell + + Returns + ------- + DefaultDict + defaultdict, with each key being a plane (e.g. 1280) and each entry + being a list of Cells """ cells_groups = defaultdict(list) for cell in cells: diff --git a/tests/data/IO/cell_io_vals/x_vals.csv b/tests/data/IO/cell_io_vals/x_vals.csv new file mode 100644 index 0000000..33d90c9 --- /dev/null +++ b/tests/data/IO/cell_io_vals/x_vals.csv @@ -0,0 +1,13 @@ +181,2117,4082,2137,2129 +2141,2130,2130,2133,2141 +2162,2127,1801,2176,2194 +2664,2435,1266,568,2103 +2098,2105,2208,2162,2221 +2081,2230,2015,2072,1413 +1950,3340,2051,3362,2046 +2046,2046,2047,2032,2923 +2064,2267,2297,2004,2356 +2357,2359,2360,2365,2612 +2623,2657,2906,2165,2685 +2892,2896,2894,2901,2270 +1924,2505,2531,2542,2536 diff --git a/tests/data/IO/cell_io_vals/y_vals.csv b/tests/data/IO/cell_io_vals/y_vals.csv new file mode 100644 index 0000000..b21efdf --- /dev/null +++ b/tests/data/IO/cell_io_vals/y_vals.csv @@ -0,0 +1,13 @@ +2379,133,332,134,133 +134,132,133,136,136 +130,1026,336,134,134 +336,343,3427,2012,138 +138,140,135,134,137 +142,139,1045,141,2045 +1048,142,142,145,150 +151,151,151,146,147 +147,147,1068,149,521 +522,523,523,525,520 +520,521,522,524,522 +526,525,525,526,524 +974,522,527,524,523 diff --git a/tests/data/IO/cell_io_vals/z_vals.csv b/tests/data/IO/cell_io_vals/z_vals.csv new file mode 100644 index 0000000..5fec792 --- /dev/null +++ b/tests/data/IO/cell_io_vals/z_vals.csv @@ -0,0 +1,13 @@ +1282,1276,1281,1286,1276 +1278,1277,1278,1285,1297 +1274,1294,1290,1279,1280 +1296,1290,1294,1294,1285 +1279,1289,1275,1286,1281 +1284,1275,1291,1273,1294 +1294,1274,1274,1274,1283 +1287,1290,1294,1272,1275 +1275,1274,1292,1273,1288 +1286,1287,1289,1298,1274 +1274,1273,1275,1275,1278 +1286,1278,1288,1295,1279 +1282,1275,1276,1275,1286 diff --git a/tests/tests/conftest.py b/tests/tests/conftest.py new file mode 100644 index 0000000..65a23bd --- /dev/null +++ b/tests/tests/conftest.py @@ -0,0 +1,9 @@ +from pathlib import Path + +import pytest + + +@pytest.fixture +def data_path(): + """Directory storing all test data""" + return Path(__file__).parent.parent / "data" diff --git a/tests/tests/test_IO/test_cell_io.py b/tests/tests/test_IO/test_cell_io.py index 9b44eee..02b543c 100644 --- a/tests/tests/test_IO/test_cell_io.py +++ b/tests/tests/test_IO/test_cell_io.py @@ -1,5 +1,3 @@ -import os - import pandas as pd import pytest from natsort import natsorted @@ -7,297 +5,83 @@ from brainglobe_utils.cells.cells import Cell from brainglobe_utils.IO import cells as cell_io -data_dir = os.path.join("tests", "data") -xml_path = os.path.join(data_dir, "cells", "cells.xml") -yml_path = os.path.join(data_dir, "cells", "cells.yml") -cubes_dir = os.path.join(data_dir, "cube_extract", "cubes") -roi_sorter_output_dir = os.path.join(data_dir, "IO", "roi_sorter_output") - -type_vals = [ - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, -] -x_vals = [ - 181, - 2117, - 4082, - 2137, - 2129, - 2141, - 2130, - 2130, - 2133, - 2141, - 2162, - 2127, - 1801, - 2176, - 2194, - 2664, - 2435, - 1266, - 568, - 2103, - 2098, - 2105, - 2208, - 2162, - 2221, - 2081, - 2230, - 2015, - 2072, - 1413, - 1950, - 3340, - 2051, - 3362, - 2046, - 2046, - 2046, - 2047, - 2032, - 2923, - 2064, - 2267, - 2297, - 2004, - 2356, - 2357, - 2359, - 2360, - 2365, - 2612, - 2623, - 2657, - 2906, - 2165, - 2685, - 2892, - 2896, - 2894, - 2901, - 2270, - 1924, - 2505, - 2531, - 2542, - 2536, -] -y_vals = [ - 2379, - 133, - 332, - 134, - 133, - 134, - 132, - 133, - 136, - 136, - 130, - 1026, - 336, - 134, - 134, - 336, - 343, - 3427, - 2012, - 138, - 138, - 140, - 135, - 134, - 137, - 142, - 139, - 1045, - 141, - 2045, - 1048, - 142, - 142, - 145, - 150, - 151, - 151, - 151, - 146, - 147, - 147, - 147, - 1068, - 149, - 521, - 522, - 523, - 523, - 525, - 520, - 520, - 521, - 522, - 524, - 522, - 526, - 525, - 525, - 526, - 524, - 974, - 522, - 527, - 524, - 523, -] -z_vals = [ - 1282, - 1276, - 1281, - 1286, - 1276, - 1278, - 1277, - 1278, - 1285, - 1297, - 1274, - 1294, - 1290, - 1279, - 1280, - 1296, - 1290, - 1294, - 1294, - 1285, - 1279, - 1289, - 1275, - 1286, - 1281, - 1284, - 1275, - 1291, - 1273, - 1294, - 1294, - 1274, - 1274, - 1274, - 1283, - 1287, - 1290, - 1294, - 1272, - 1275, - 1275, - 1274, - 1292, - 1273, - 1288, - 1286, - 1287, - 1289, - 1298, - 1274, - 1274, - 1273, - 1275, - 1275, - 1278, - 1286, - 1278, - 1288, - 1295, - 1279, - 1282, - 1275, - 1276, - 1275, - 1286, -] - -cubes_cells = [ - Cell([340, 1004, 15], 1), - Cell([340, 1004, 15], 1), - Cell([392, 522, 10], 1), - Cell([392, 522, 10], 1), -] - -roi_sorter_cells = [ - Cell([4056, 564, 358], 1), - Cell([3989, 267, 570], 1), - Cell([4351, 735, 439], 1), - Cell([4395, 677, 367], 1), -] - - -def test_get_cells(): + +@pytest.fixture +def xml_path(data_path): + return str(data_path / "cells" / "cells.xml") + + +@pytest.fixture +def yml_path(data_path): + return str(data_path / "cells" / "cells.yml") + + +@pytest.fixture +def cubes_dir(data_path): + return str(data_path / "cube_extract" / "cubes") + + +@pytest.fixture +def roi_sorter_output_dir(data_path): + return str(data_path / "IO" / "roi_sorter_output") + + +@pytest.fixture +def type_vals(): + return [1] * 65 + + +def read_csv_as_list(csv_path): + df = pd.read_csv(csv_path, header=None) + return df.to_numpy().flatten().tolist() + + +@pytest.fixture +def x_vals(data_path): + csv_path = data_path / "IO" / "cell_io_vals" / "x_vals.csv" + return read_csv_as_list(csv_path) + + +@pytest.fixture +def y_vals(data_path): + csv_path = data_path / "IO" / "cell_io_vals" / "y_vals.csv" + return read_csv_as_list(csv_path) + + +@pytest.fixture +def z_vals(data_path): + csv_path = data_path / "IO" / "cell_io_vals" / "z_vals.csv" + return read_csv_as_list(csv_path) + + +@pytest.fixture +def cubes_cells(): + return [ + Cell([340, 1004, 15], 1), + Cell([340, 1004, 15], 1), + Cell([392, 522, 10], 1), + Cell([392, 522, 10], 1), + ] + + +@pytest.fixture +def roi_sorter_cells(): + return [ + Cell([4056, 564, 358], 1), + Cell([3989, 267, 570], 1), + Cell([4351, 735, 439], 1), + Cell([4395, 677, 367], 1), + ] + + +def test_get_cells( + cubes_cells, + roi_sorter_cells, + xml_path, + yml_path, + cubes_dir, + roi_sorter_output_dir, +): cells = cell_io.get_cells(xml_path) assert len(cells) == 65 assert Cell([2536, 523, 1286], 1) == cells[64] @@ -318,7 +102,7 @@ def test_get_cells(): assert cell_io.get_cells("misc_format.abc") -def assert_cells_csv(csv_path): +def assert_cells_csv(csv_path, x_vals, y_vals, z_vals, type_vals): cells_df = pd.read_csv(csv_path) assert len(cells_df) == 65 assert cells_df.type.tolist() == type_vals @@ -327,7 +111,7 @@ def assert_cells_csv(csv_path): assert cells_df.z.tolist() == z_vals -def test_save_cells(tmp_path): +def test_save_cells(tmp_path, xml_path, x_vals, y_vals, z_vals, type_vals): cells = cell_io.get_cells(xml_path) tmp_cells_out_path = tmp_path / "cells.xml" cell_io.save_cells(cells, tmp_cells_out_path, save_csv=True) @@ -335,17 +119,17 @@ def test_save_cells(tmp_path): tmp_cells_out_path = tmp_path / "cells.csv" cell_io.cells_to_csv(cells, tmp_cells_out_path) - assert_cells_csv(tmp_cells_out_path) + assert_cells_csv(tmp_cells_out_path, x_vals, y_vals, z_vals, type_vals) -def test_cells_to_xml(tmp_path): +def test_cells_to_xml(tmp_path, xml_path): cells = cell_io.get_cells(xml_path) tmp_cells_out_path = tmp_path / "cells.xml" cell_io.cells_to_xml(cells, tmp_cells_out_path) assert cells == cell_io.get_cells(str(tmp_cells_out_path)) -def test_cells_xml_to_dataframe(): +def test_cells_xml_to_dataframe(xml_path, x_vals, y_vals, z_vals, type_vals): cells_df = cell_io.cells_xml_to_df(xml_path) assert len(cells_df) == 65 assert cells_df.type.tolist() == type_vals @@ -354,8 +138,8 @@ def test_cells_xml_to_dataframe(): assert cells_df.z.tolist() == z_vals -def test_cells_to_csv(tmp_path): +def test_cells_to_csv(tmp_path, xml_path, x_vals, y_vals, z_vals, type_vals): cells = cell_io.get_cells(xml_path) tmp_cells_out_path = tmp_path / "cells.csv" cell_io.cells_to_csv(cells, tmp_cells_out_path) - assert_cells_csv(tmp_cells_out_path) + assert_cells_csv(tmp_cells_out_path, x_vals, y_vals, z_vals, type_vals) diff --git a/tests/tests/test_IO/test_surfaces.py b/tests/tests/test_IO/test_surfaces.py index 63d6ba9..f3d88c8 100644 --- a/tests/tests/test_IO/test_surfaces.py +++ b/tests/tests/test_IO/test_surfaces.py @@ -1,13 +1,13 @@ -from pathlib import Path - +import pytest from skimage import measure from skimage.draw import ellipsoid from brainglobe_utils.IO.surfaces import marching_cubes_to_obj -test_obj = ( - Path(__file__).parent.parent.parent / "data" / "IO" / "obj" / "test.obj" -) + +@pytest.fixture +def test_obj(data_path): + return data_path / "IO" / "obj" / "test.obj" def compare_text_files(a, b): @@ -18,7 +18,7 @@ def compare_text_files(a, b): assert a_contents == b_contents -def test_marching_cubes_to_obj(tmp_path): +def test_marching_cubes_to_obj(tmp_path, test_obj): output_file = tmp_path / "test.obj" ellip_base = ellipsoid(6, 10, 16, levelset=True) marching_cubes_out = measure.marching_cubes(ellip_base, 0) diff --git a/tests/tests/test_IO/test_yaml_io.py b/tests/tests/test_IO/test_yaml_io.py index aa68c7d..79524c4 100644 --- a/tests/tests/test_IO/test_yaml_io.py +++ b/tests/tests/test_IO/test_yaml_io.py @@ -1,29 +1,39 @@ from pathlib import Path +import pytest + from brainglobe_utils.IO import yaml -data_dir = Path("tests", "data") -yaml_file = data_dir / "yaml" / "single_section.yml" -yaml_section = [ - {"a": "/some_path", "b": "Eeyore", "d": "a_single_dog", "e": 2}, - {"a": "/another/path", "z": 1, 2: 2}, -] +@pytest.fixture +def yaml_file(data_path): + return data_path / "yaml" / "single_section.yml" + + +@pytest.fixture +def yaml_section(): + return [ + {"a": "/some_path", "b": "Eeyore", "d": "a_single_dog", "e": 2}, + {"a": "/another/path", "z": 1, 2: 2}, + ] + -yaml_contents = {"data": yaml_section} +@pytest.fixture +def yaml_contents(yaml_section): + return {"data": yaml_section} -def test_open_yaml(): +def test_open_yaml(yaml_file, yaml_contents): yaml_contents_test = yaml.open_yaml(yaml_file) assert yaml_contents_test == yaml_contents -def test_read_yaml_section(section="data"): +def test_read_yaml_section(yaml_file, yaml_section, section="data"): yaml_section_test = yaml.read_yaml_section(yaml_file, section) assert yaml_section_test == yaml_section -def test_save_yaml(tmpdir): +def test_save_yaml(tmpdir, yaml_file, yaml_contents): test_yaml_path = Path(tmpdir) / "test.yml" yaml.save_yaml(yaml_contents, test_yaml_path) test_yaml = yaml.open_yaml(test_yaml_path) diff --git a/tests/tests/test_brainmapper/test_analysis.py b/tests/tests/test_brainmapper/test_analysis.py index 921909e..f20d9eb 100644 --- a/tests/tests/test_brainmapper/test_analysis.py +++ b/tests/tests/test_brainmapper/test_analysis.py @@ -132,9 +132,9 @@ def atlas(tmp_path): @pytest.fixture -def brainmapper_data_path(): +def brainmapper_data_path(data_path): """Directory storing all brainmapper test data""" - return Path(__file__).parent.parent.parent / "data" / "brainmapper" + return data_path / "brainmapper" @pytest.fixture diff --git a/tests/tests/test_cells/test_cells.py b/tests/tests/test_cells/test_cells.py index c556a5e..49f0972 100644 --- a/tests/tests/test_cells/test_cells.py +++ b/tests/tests/test_cells/test_cells.py @@ -1,16 +1,23 @@ import os +import pytest from natsort import natsorted from brainglobe_utils.cells import cells from brainglobe_utils.IO.cells import get_cells -data_dir = os.path.join("tests", "data") -cubes_dir = os.path.join(data_dir, "cube_extract", "cubes") -xml_path = os.path.join(data_dir, "cells", "cells.xml") +@pytest.fixture +def cubes_dir(data_path): + return str(data_path / "cube_extract" / "cubes") -def test_pos_from_file_name(): + +@pytest.fixture +def xml_path(data_path): + return str(data_path / "cells" / "cells.xml") + + +def test_pos_from_file_name(cubes_dir): positions_validate = [ [392, 522, 10], [340, 1004, 15], @@ -24,7 +31,7 @@ def test_pos_from_file_name(): assert natsorted(positions) == natsorted(positions_validate) -def test_group_cells_by_z(): +def test_group_cells_by_z(xml_path): z_planes_validate = [ 1272, 1273, diff --git a/tests/tests/test_general/test_list.py b/tests/tests/test_general/test_list.py index 6572560..80455b2 100644 --- a/tests/tests/test_general/test_list.py +++ b/tests/tests/test_general/test_list.py @@ -1,14 +1,19 @@ -from brainglobe_utils.general import list as list_tools - -a = [1, "a", 10, 30] -b = [30, 10, "c", "d"] +import pytest - -list_with_empty = ["test1", "test 2", " ", "", "test4", ""] -list_without_empty = ["test1", "test 2", " ", "test4"] +from brainglobe_utils.general import list as list_tools -def test_remove_empty_string(): +@pytest.mark.parametrize( + "list_with_empty, list_without_empty", + [ + pytest.param( + ["test1", "test 2", " ", "", "test4", ""], + ["test1", "test 2", " ", "test4"], + id="remove empty string", + ), + ], +) +def test_remove_empty_string(list_with_empty, list_without_empty): assert ( list_tools.remove_empty_string(list_with_empty) == list_without_empty ) @@ -27,5 +32,11 @@ def test_check_unique_list(): assert (False, [3, "dog"]) == list_tools.check_unique_list(repeating_list) -def test_common_member(): +@pytest.mark.parametrize( + "a, b", + [ + pytest.param([1, "a", 10, 30], [30, 10, "c", "d"], id="common 10 30"), + ], +) +def test_common_member(a, b): assert (True, [10, 30]) == list_tools.common_member(a, b) diff --git a/tests/tests/test_general/test_string.py b/tests/tests/test_general/test_string.py index 0f757ce..eaced21 100644 --- a/tests/tests/test_general/test_string.py +++ b/tests/tests/test_general/test_string.py @@ -1,43 +1,48 @@ -from pathlib import Path +import pytest from brainglobe_utils.general import string -data_dir = Path("tests", "data") -jabberwocky = data_dir / "general" / "jabberwocky.txt" -jabberwocky_list = [ - "’Twas brillig, and the slithy toves", - "Did gyre and gimble in the wabe:", - "All mimsy were the borogoves,", - "And the mome raths outgrabe.", - "“Beware the Jabberwock, my son!", - "The jaws that bite, the claws that catch!", - "Beware the Jubjub bird, and shun", - "The frumious Bandersnatch!”", - "He took his vorpal sword in hand;", - "Long time the manxome foe he sought—", - "So rested he by the Tumtum tree", - "And stood awhile in thought.", - "And, as in uffish thought he stood,", - "The Jabberwock, with eyes of flame,", - "Came whiffling through the tulgey wood,", - "And burbled as it came!", - "One, two! One, two! And through and through", - "The vorpal blade went snicker-snack!", - "He left it dead, and with its head", - "He went galumphing back.", - "“And hast thou slain the Jabberwock?", - "Come to my arms, my beamish boy!", - "O frabjous day! Callooh! Callay!”", - "He chortled in his joy.", - "’Twas brillig, and the slithy toves", - "Did gyre and gimble in the wabe:", - "All mimsy were the borogoves,", - "And the mome raths outgrabe.", -] +@pytest.fixture +def jabberwocky(data_path): + return data_path / "general" / "jabberwocky.txt" -def test_get_string_lines(): +@pytest.fixture +def jabberwocky_list(): + return [ + "’Twas brillig, and the slithy toves", + "Did gyre and gimble in the wabe:", + "All mimsy were the borogoves,", + "And the mome raths outgrabe.", + "“Beware the Jabberwock, my son!", + "The jaws that bite, the claws that catch!", + "Beware the Jubjub bird, and shun", + "The frumious Bandersnatch!”", + "He took his vorpal sword in hand;", + "Long time the manxome foe he sought—", + "So rested he by the Tumtum tree", + "And stood awhile in thought.", + "And, as in uffish thought he stood,", + "The Jabberwock, with eyes of flame,", + "Came whiffling through the tulgey wood,", + "And burbled as it came!", + "One, two! One, two! And through and through", + "The vorpal blade went snicker-snack!", + "He left it dead, and with its head", + "He went galumphing back.", + "“And hast thou slain the Jabberwock?", + "Come to my arms, my beamish boy!", + "O frabjous day! Callooh! Callay!”", + "He chortled in his joy.", + "’Twas brillig, and the slithy toves", + "Did gyre and gimble in the wabe:", + "All mimsy were the borogoves,", + "And the mome raths outgrabe.", + ] + + +def test_get_string_lines(jabberwocky, jabberwocky_list): assert string.get_text_lines(jabberwocky) == jabberwocky_list assert ( string.get_text_lines(jabberwocky, return_lines=8) diff --git a/tests/tests/test_general/test_system.py b/tests/tests/test_general/test_system.py index ed8138e..16ef4cf 100644 --- a/tests/tests/test_general/test_system.py +++ b/tests/tests/test_general/test_system.py @@ -11,27 +11,40 @@ from brainglobe_utils.general.exceptions import CommandLineInputError from brainglobe_utils.general.string import get_text_lines -data_dir = Path("tests", "data") -cubes_dir = data_dir / "cubes" -jabberwocky = data_dir / "general" / "jabberwocky.txt" -jabberwocky_sorted = data_dir / "general" / "jabberwocky_sorted.txt" - -cubes = [ - "pCellz222y2805x9962Ch1.tif", - "pCellz222y2805x9962Ch2.tif", - "pCellz258y3892x10559Ch1.tif", - "pCellz258y3892x10559Ch2.tif", - "pCellz413y2308x9391Ch1.tif", - "pCellz413y2308x9391Ch2.tif", - "pCellz416y2503x5997Ch1.tif", - "pCellz416y2503x5997Ch2.tif", - "pCellz418y5457x9489Ch1.tif", - "pCellz418y5457x9489Ch2.tif", - "pCellz433y4425x7552Ch1.tif", - "pCellz433y4425x7552Ch2.tif", -] - -sorted_cubes_dir = [os.path.join(str(cubes_dir), cube) for cube in cubes] + +@pytest.fixture +def cubes_dir(data_path): + return data_path / "cubes" + + +@pytest.fixture +def jabberwocky(data_path): + return data_path / "general" / "jabberwocky.txt" + + +@pytest.fixture +def jabberwocky_sorted(data_path): + return data_path / "general" / "jabberwocky_sorted.txt" + + +@pytest.fixture +def sorted_cubes_dir(cubes_dir): + cubes = [ + "pCellz222y2805x9962Ch1.tif", + "pCellz222y2805x9962Ch2.tif", + "pCellz258y3892x10559Ch1.tif", + "pCellz258y3892x10559Ch2.tif", + "pCellz413y2308x9391Ch1.tif", + "pCellz413y2308x9391Ch2.tif", + "pCellz416y2503x5997Ch1.tif", + "pCellz416y2503x5997Ch2.tif", + "pCellz418y5457x9489Ch1.tif", + "pCellz418y5457x9489Ch2.tif", + "pCellz433y4425x7552Ch1.tif", + "pCellz433y4425x7552Ch2.tif", + ] + + return [str(cubes_dir / cube) for cube in cubes] @pytest.fixture @@ -78,7 +91,9 @@ def test_ensure_directory_exists(tmpdir): exist_dir_pathlib.rmdir() -def test_get_sorted_file_paths(): +def test_get_sorted_file_paths( + cubes_dir, jabberwocky, jabberwocky_sorted, sorted_cubes_dir +): # test list shuffled = sorted_cubes_dir.copy() shuffle(shuffled) @@ -102,8 +117,8 @@ def test_get_sorted_file_paths(): system.get_sorted_file_paths(shuffled[0]) -def test_check_path_in_dir(): - assert system.check_path_in_dir(jabberwocky, data_dir / "general") +def test_check_path_in_dir(jabberwocky, data_path): + assert system.check_path_in_dir(jabberwocky, data_path / "general") def test_get_num_processes(): diff --git a/tests/tests/test_image/test_heatmap.py b/tests/tests/test_image/test_heatmap.py index 8b91bf1..ad1c84e 100644 --- a/tests/tests/test_image/test_heatmap.py +++ b/tests/tests/test_image/test_heatmap.py @@ -1,15 +1,18 @@ -from pathlib import Path - import numpy as np import pytest from tifffile import imread from brainglobe_utils.image.heatmap import heatmap_from_points, rescale_array -data_dir = Path("tests", "data") -heatmap_validate_path = data_dir / "image" / "heatmap.tif" -points = np.array([[5, 5, 5], [10, 10, 10], [15, 15, 15]]) +@pytest.fixture +def heatmap_validate_path(data_path): + return data_path / "image" / "heatmap.tif" + + +@pytest.fixture +def points(): + return np.array([[5, 5, 5], [10, 10, 10], [15, 15, 15]]) @pytest.fixture @@ -29,7 +32,9 @@ def test_rescale_array(mask_array): assert resized_array.shape == small_array.shape -def test_heatmap_from_points(tmp_path, mask_array): +def test_heatmap_from_points( + tmp_path, mask_array, points, heatmap_validate_path +): output_filename = tmp_path / "test_heatmap.tif" heatmap_test = heatmap_from_points( diff --git a/tests/tests/test_image/test_masking.py b/tests/tests/test_image/test_masking.py index 0b2ed5f..4d220c7 100644 --- a/tests/tests/test_image/test_masking.py +++ b/tests/tests/test_image/test_masking.py @@ -1,39 +1,49 @@ import numpy as np +import pytest import brainglobe_utils.image.masking as masking -raw_image = np.array( - [ - [1, 1, 3, 3, 1, 1], - [1, 1, 5, 5, 1, 1], - [1, 1, 5, 5, 1, 1], - [1, 1, 5, 5, 1, 1], - ] -) - -mask_val_4 = np.array( - [ - [0, 0, 0, 0, 0, 0], - [0, 0, 1, 1, 0, 0], - [0, 0, 1, 1, 0, 0], - [0, 0, 1, 1, 0, 0], - ] -) - -masked_image = np.array( - [ - [0, 0, 0, 0, 0, 0], - [0, 0, 5, 5, 0, 0], - [0, 0, 5, 5, 0, 0], - [0, 0, 5, 5, 0, 0], - ] -) - - -def test_make_mask(): + +@pytest.fixture +def raw_image(): + return np.array( + [ + [1, 1, 3, 3, 1, 1], + [1, 1, 5, 5, 1, 1], + [1, 1, 5, 5, 1, 1], + [1, 1, 5, 5, 1, 1], + ] + ) + + +@pytest.fixture +def mask_val_4(): + return np.array( + [ + [0, 0, 0, 0, 0, 0], + [0, 0, 1, 1, 0, 0], + [0, 0, 1, 1, 0, 0], + [0, 0, 1, 1, 0, 0], + ] + ) + + +@pytest.fixture +def masked_image(): + return np.array( + [ + [0, 0, 0, 0, 0, 0], + [0, 0, 5, 5, 0, 0], + [0, 0, 5, 5, 0, 0], + [0, 0, 5, 5, 0, 0], + ] + ) + + +def test_make_mask(mask_val_4, raw_image): assert (mask_val_4 == masking.make_mask(raw_image, threshold=4)).all() -def test_mask_image_threshold(): +def test_mask_image_threshold(raw_image, masked_image): result = masking.mask_image_threshold(raw_image, raw_image, threshold=4) assert (result == masked_image).all() diff --git a/tests/tests/test_image/test_scale.py b/tests/tests/test_image/test_scale.py index 7007605..754040c 100644 --- a/tests/tests/test_image/test_scale.py +++ b/tests/tests/test_image/test_scale.py @@ -1,18 +1,26 @@ import numpy as np +import pytest from brainglobe_utils.image import scale -test_2d_img = np.array([[1, 2, 10, 100], [5, 25, 300, 1000], [1, 0, 0, 125]]) -validate_2d_img = np.array( - [ - [65.535, 131.07, 655.35, 6553.5], - [327.675, 1638.375, 19660.5, 65535], - [65.535, 0, 0, 8191.875], - ] -) +@pytest.fixture +def test_2d_img(): + return np.array([[1, 2, 10, 100], [5, 25, 300, 1000], [1, 0, 0, 125]]) -def test_scale_to_16_bits(): + +@pytest.fixture +def validate_2d_img(): + return np.array( + [ + [65.535, 131.07, 655.35, 6553.5], + [327.675, 1638.375, 19660.5, 65535], + [65.535, 0, 0, 8191.875], + ] + ) + + +def test_scale_to_16_bits(test_2d_img, validate_2d_img): validate_2d_img_uint16 = validate_2d_img.astype(np.uint16, copy=False) assert ( validate_2d_img_uint16 diff --git a/tests/tests/test_pandas/test_pandas_misc.py b/tests/tests/test_pandas/test_pandas_misc.py index 3a3a7ac..7284fe6 100644 --- a/tests/tests/test_pandas/test_pandas_misc.py +++ b/tests/tests/test_pandas/test_pandas_misc.py @@ -1,13 +1,22 @@ import numpy as np import pandas as pd +import pytest from brainglobe_utils.pandas import misc as pandas_misc -columns = ["name", "number"] -data_with_nan = [["one", np.nan], ["two", 15], ["three", np.nan]] -df_with_nan = pd.DataFrame(data_with_nan, columns=columns) -data_with_inf = [["one", np.inf], ["two", 15], ["three", np.inf]] -df_with_inf = pd.DataFrame(data_with_inf, columns=columns) + +@pytest.fixture +def df_with_inf(): + columns = ["name", "number"] + data_with_inf = [["one", np.inf], ["two", 15], ["three", np.inf]] + return pd.DataFrame(data_with_inf, columns=columns) + + +@pytest.fixture +def df_with_nan(): + columns = ["name", "number"] + data_with_nan = [["one", np.nan], ["two", 15], ["three", np.nan]] + return pd.DataFrame(data_with_nan, columns=columns) def test_initialise_df(): @@ -16,7 +25,7 @@ def test_initialise_df(): assert df.equals(test_df) -def test_sanitise_df(): +def test_sanitise_df(df_with_nan, df_with_inf): sanitised_df = pandas_misc.sanitise_df(df_with_inf) assert sanitised_df.equals(df_with_nan)