From 5b089424c9a3d64cc0ffb08024bc82135799ecc2 Mon Sep 17 00:00:00 2001 From: MikeG Date: Tue, 26 Mar 2024 14:56:34 +0100 Subject: [PATCH] Cleanup app tests (#78) * line up app test arguments * use a function to write json files * `Test_mtype_densities_from_probability_map` doesn't need to be a class * use simple assert for shapes --- tests/app/test_cell_densities.py | 214 +++++++----------- tests/app/test_combination.py | 65 +++--- tests/app/test_mtype_densities.py | 151 +++++------- ...est_refined_inhibitory_neuron_densities.py | 30 +-- tests/utils.py | 7 + 5 files changed, 187 insertions(+), 280 deletions(-) create mode 100644 tests/utils.py diff --git a/tests/app/test_cell_densities.py b/tests/app/test_cell_densities.py index 2730222..4c8463a 100644 --- a/tests/app/test_cell_densities.py +++ b/tests/app/test_cell_densities.py @@ -34,28 +34,13 @@ from tests.densities.test_measurement_to_density import ( get_input_data as get_measurement_to_density_input_data, ) +from tests.utils import write_json TEST_PATH = Path(__file__).parent.parent DATA_PATH = TEST_PATH.parent / "atlas_densities" / "app" / "data" MEASUREMENTS_PATH = DATA_PATH / "measurements" -def _get_cell_density_result(runner): - args = [ - "cell-density", - "--hierarchy-path", - str(Path(TEST_PATH, "1.json")), - "--annotation-path", - "annotation.nrrd", - "--nissl-path", - "nissl.nrrd", - "--output-path", - "overall_cell_density.nrrd", - ] - - return runner.invoke(tested.app, args) - - def test_cell_density(): input_ = { "annotation": np.array( @@ -78,45 +63,46 @@ def test_cell_density(): with runner.isolated_filesystem(): for name, array in input_.items(): VoxelData(array, voxel_dimensions=voxel_dimensions).save_nrrd(f"{name}.nrrd") - result = _get_cell_density_result(runner) + + args = [ + # fmt: off + "cell-density", + "--hierarchy-path", TEST_PATH / "1.json", + "--annotation-path", "annotation.nrrd", + "--nissl-path", "nissl.nrrd", + "--output-path", "overall_cell_density.nrrd", + # fmt: on + ] + + result = runner.invoke(tested.app, args) assert result.exit_code == 0 + voxel_data = VoxelData.load_nrrd("overall_cell_density.nrrd") assert voxel_data.raw.dtype == float # An error should be raised if annotation and nissl don't use the same voxel dimensions VoxelData(np.ones((3, 1, 3)), voxel_dimensions=[10] * 3).save_nrrd("nissl.nrrd") - result = _get_cell_density_result(runner) + result = runner.invoke(tested.app, args) assert "voxel_dimensions" in str(result.exception) -def _get_glia_cell_densities_result(runner): +def test_glia_cell_densities(): args = [ + # fmt: off "glia-cell-densities", - "--annotation-path", - "annotation.nrrd", - "--hierarchy-path", - str(Path(TEST_PATH, "1.json")), - "--cell-density-path", - "cell_density.nrrd", - "--glia-density-path", - "glia_density.nrrd", - "--astrocyte-density-path", - "astrocyte_density.nrrd", - "--oligodendrocyte-density-path", - "oligodendrocyte_density.nrrd", - "--microglia-density-path", - "microglia_density.nrrd", - "--glia-proportions-path", - "glia_proportions.json", - "--output-dir", - "densities", + "--annotation-path", "annotation.nrrd", + "--hierarchy-path", TEST_PATH / "1.json", + "--cell-density-path", "cell_density.nrrd", + "--glia-density-path", "glia_density.nrrd", + "--astrocyte-density-path", "astrocyte_density.nrrd", + "--oligodendrocyte-density-path", "oligodendrocyte_density.nrrd", + "--microglia-density-path", "microglia_density.nrrd", + "--glia-proportions-path", "glia_proportions.json", + "--output-dir", "densities", + # fmt: on ] - return runner.invoke(tested.app, args) - - -def test_glia_cell_densities(): glia_cell_count = sum(glia_cell_counts().values()) input_ = get_glia_input_data(glia_cell_count) runner = CliRunner() @@ -132,25 +118,26 @@ def test_glia_cell_densities(): VoxelData(unconstrained_density, voxel_dimensions=voxel_dimensions).save_nrrd( glia_type + "_density.nrrd" ) - with open("glia_proportions.json", "w", encoding="utf-8") as out: - json.dump(input_["glia_proportions"], out) - result = _get_glia_cell_densities_result(runner) + + write_json("glia_proportions.json", input_["glia_proportions"]) + + result = runner.invoke(tested.app, args) assert result.exit_code == 0 neuron_density = VoxelData.load_nrrd("densities/neuron_density.nrrd") assert neuron_density.raw.dtype == np.float64 - npt.assert_array_equal(neuron_density.shape, input_["annotation"].shape) + assert neuron_density.shape == input_["annotation"].shape assert np.all(neuron_density.raw >= 0.0) oligodendrocyte_density = VoxelData.load_nrrd("densities/oligodendrocyte_density.nrrd") assert oligodendrocyte_density.raw.dtype == np.float64 - npt.assert_array_equal(neuron_density.shape, input_["annotation"].shape) + assert neuron_density.shape == input_["annotation"].shape # Check that an exception is thrown if voxel dimensions aren't consistent VoxelData(input_["cell_density"], voxel_dimensions=(10, 10, 10)).save_nrrd( "cell_density.nrrd" ) - result = _get_glia_cell_densities_result(runner) + result = runner.invoke(tested.app, args) assert "voxel_dimensions" in str(result.exception) # Check that an exception is thrown if the input cell density has negative values @@ -158,31 +145,23 @@ def test_glia_cell_densities(): VoxelData(input_["cell_density"], voxel_dimensions=(10, 10, 10)).save_nrrd( "cell_density.nrrd" ) - result = _get_glia_cell_densities_result(runner) + result = runner.invoke(tested.app, args) assert "Negative density value" in str(result.exception) -def _get_inh_and_exc_neuron_densities_result(runner): +def test_inhibitory_and_excitatory_neuron_densities(): args = [ + # fmt: off "inhibitory-and-excitatory-neuron-densities", - "--annotation-path", - "annotation.nrrd", - "--hierarchy-path", - str(Path(TEST_PATH, "1.json")), - "--gad1-path", - "gad1.nrrd", - "--nrn1-path", - "nrn1.nrrd", - "--neuron-density-path", - "neuron_density.nrrd", - "--output-dir", - "densities", + "--annotation-path", "annotation.nrrd", + "--hierarchy-path", TEST_PATH / "1.json", + "--gad1-path", "gad1.nrrd", + "--nrn1-path", "nrn1.nrrd", + "--neuron-density-path", "neuron_density.nrrd", + "--output-dir", "densities", + # fmt: on ] - return runner.invoke(tested.app, args) - - -def test_inhibitory_and_excitatory_neuron_densities(): inhibitory_df = extract_inhibitory_neurons_dataframe(Path(MEASUREMENTS_PATH, "mmc1.xlsx")) neuron_count = inhibitory_data(inhibitory_df)["neuron_count"] input_ = get_inhibitory_neuron_input_data(neuron_count) @@ -192,24 +171,24 @@ def test_inhibitory_and_excitatory_neuron_densities(): for name in ["annotation", "neuron_density", "gad1", "nrn1"]: VoxelData(input_[name], voxel_dimensions=voxel_dimensions).save_nrrd(name + ".nrrd") - result = _get_inh_and_exc_neuron_densities_result(runner) + result = runner.invoke(tested.app, args) assert result.exit_code == 0 inh_neuron_density = VoxelData.load_nrrd("densities/inhibitory_neuron_density.nrrd") assert inh_neuron_density.raw.dtype == np.float64 - npt.assert_array_equal(inh_neuron_density.shape, input_["annotation"].shape) + assert inh_neuron_density.shape == input_["annotation"].shape assert np.all(inh_neuron_density.raw >= 0.0) exc_neuron_density = VoxelData.load_nrrd("densities/excitatory_neuron_density.nrrd") assert exc_neuron_density.raw.dtype == np.float64 - npt.assert_array_equal(exc_neuron_density.shape, input_["annotation"].shape) + assert exc_neuron_density.shape == input_["annotation"].shape assert np.all(exc_neuron_density.raw >= 0.0) # Check that an exception is thrown if voxel dimensions aren't consistent VoxelData(input_["neuron_density"], voxel_dimensions=(10, 10, 10)).save_nrrd( "neuron_density.nrrd" ) - result = _get_inh_and_exc_neuron_densities_result(runner) + result = runner.invoke(tested.app, args) assert "voxel_dimensions" in str(result.exception) # Check that an exception is thrown if the input neuron density has negative values @@ -217,27 +196,22 @@ def test_inhibitory_and_excitatory_neuron_densities(): VoxelData(input_["neuron_density"], voxel_dimensions=(25, 25, 25)).save_nrrd( "neuron_density.nrrd" ) - result = _get_inh_and_exc_neuron_densities_result(runner) + result = runner.invoke(tested.app, args) assert "Negative density value" in str(result.exception) -def _get_compile_measurements_result(runner): +@pytest.mark.filterwarnings("ignore::atlas_densities.exceptions.AtlasDensitiesWarning") +def test_compile_measurements(): args = [ + # fmt: off "compile-measurements", - "--measurements-output-path", - "measurements.csv", - "--homogenous-regions-output-path", - "homogenous_regions.csv", + "--measurements-output-path", "measurements.csv", + "--homogenous-regions-output-path", "homogenous_regions.csv", + # fmt: on ] - - return runner.invoke(tested.app, args) - - -@pytest.mark.filterwarnings("ignore::atlas_densities.exceptions.AtlasDensitiesWarning") -def test_compile_measurements(): runner = CliRunner() with runner.isolated_filesystem(): - result = _get_compile_measurements_result(runner) + result = runner.invoke(tested.app, args) assert result.exit_code == 0 dataframe = pd.read_csv("measurements.csv") @@ -267,21 +241,16 @@ def test_compile_measurements(): def _get_measurements_to_average_densities_result(runner, hierarchy_path, measurements_path): args = [ + # fmt: off "measurements-to-average-densities", - "--hierarchy-path", - hierarchy_path, - "--annotation-path", - "annotation.nrrd", - "--region-name", - "Basic cell groups and regions", - "--cell-density-path", - "cell_density.nrrd", - "--neuron-density-path", - "neuron_density.nrrd", - "--measurements-path", - measurements_path, - "--output-path", - "average_densities.csv", + "--hierarchy-path", hierarchy_path, + "--annotation-path", "annotation.nrrd", + "--region-name", "Basic cell groups and regions", + "--cell-density-path", "cell_density.nrrd", + "--neuron-density-path", "neuron_density.nrrd", + "--measurements-path", measurements_path, + "--output-path", "average_densities.csv", + # fmt: on ] return runner.invoke(tested.app, args) @@ -295,8 +264,7 @@ def test_measurements_to_average_densities(): for name in ["annotation", "cell_density", "neuron_density"]: VoxelData(input_[name], voxel_dimensions=voxel_dimensions).save_nrrd(name + ".nrrd") input_["measurements"].to_csv("measurements.csv", index=False) - with open("hierarchy.json", "w", encoding="utf-8") as out: - json.dump(input_["hierarchy"], out, indent=1, separators=(",", ": ")) + write_json("hierarchy.json", input_["hierarchy"]) result = _get_measurements_to_average_densities_result( runner, hierarchy_path="hierarchy.json", measurements_path="measurements.csv" @@ -320,32 +288,21 @@ def test_measurements_to_average_densities(): assert np.all(actual["measurement_unit"] == "number of cells per mm^3") -def _get_fitting_result(runner): +@pytest.mark.filterwarnings("ignore::atlas_densities.exceptions.AtlasDensitiesWarning") +def test_fit_average_densities(): args = [ + # fmt: off "fit-average-densities", - "--hierarchy-path", - "hierarchy.json", - "--annotation-path", - "annotation.nrrd", - "--neuron-density-path", - "neuron_density.nrrd", - "--gene-config-path", - "gene_config.yaml", - "--average-densities-path", - "average_densities.csv", - "--homogenous-regions-path", - "homogenous_regions.csv", - "--fitted-densities-output-path", - "fitted_densities.csv", - "--fitting-maps-output-path", - "fitting_maps.json", + "--hierarchy-path", "hierarchy.json", + "--annotation-path", "annotation.nrrd", + "--neuron-density-path", "neuron_density.nrrd", + "--gene-config-path", "gene_config.yaml", + "--average-densities-path", "average_densities.csv", + "--homogenous-regions-path", "homogenous_regions.csv", + "--fitted-densities-output-path", "fitted_densities.csv", + "--fitting-maps-output-path", "fitting_maps.json", + # fmt: on ] - - return runner.invoke(tested.app, args) - - -@pytest.mark.filterwarnings("ignore::atlas_densities.exceptions.AtlasDensitiesWarning") -def test_fit_average_densities(): runner = CliRunner() with runner.isolated_filesystem(): input_ = get_fitting_input_data() @@ -359,14 +316,9 @@ def test_fit_average_densities(): input_["homogenous_regions"].to_csv("homogenous_regions.csv", index=False) input_["average_densities"].to_csv("average_densities.csv", index=False) - with open("hierarchy.json", "w", encoding="utf-8") as out: - json.dump(input_["hierarchy"], out, indent=1, separators=(",", ": ")) - - with open("realigned_slices.json", "w", encoding="utf-8") as out: - json.dump(input_["realigned_slices"], out, indent=1, separators=(",", ": ")) - - with open("std_cells.json", "w", encoding="utf-8") as out: - json.dump(input_["cell_density_stddevs"], out, indent=1, separators=(",", ": ")) + write_json("hierarchy.json", input_["hierarchy"]) + write_json("realigned_slices.json", input_["realigned_slices"]) + write_json("std_cells.json", input_["cell_density_stddevs"]) with open("gene_config.yaml", "w", encoding="utf-8") as out: gene_config = { @@ -386,7 +338,7 @@ def test_fit_average_densities(): yaml.dump(gene_config, out) - result = _get_fitting_result(runner) + result = runner.invoke(tested.app, args) assert result.exit_code == 0 densities = pd.read_csv("fitted_densities.csv") @@ -411,7 +363,7 @@ def test_fit_average_densities(): VoxelData(input_["neuron_density"], voxel_dimensions=(10, 10, 10)).save_nrrd( "neuron_density.nrrd" ) - result = _get_fitting_result(runner) + result = runner.invoke(tested.app, args) assert "Negative density value" in str(result.exception) diff --git a/tests/app/test_combination.py b/tests/app/test_combination.py index b696697..12a70f0 100644 --- a/tests/app/test_combination.py +++ b/tests/app/test_combination.py @@ -104,15 +104,13 @@ def test_combine_markers(tmp_path): result = runner.invoke( tested.app, [ - "--log-output-path", - str(td), + # fmt: off + "--log-output-path", td, "combine-markers", - "--annotation-path", - "annotation.nrrd", - "--hierarchy-path", - str(TEST_PATH / "1.json"), - "--config", - str(TEST_PATH / "markers_config.yaml"), + "--annotation-path", "annotation.nrrd", + "--hierarchy-path", TEST_PATH / "1.json", + "--config", TEST_PATH / "markers_config.yaml", + # fmt: on ], catch_exceptions=False, ) @@ -145,22 +143,17 @@ def test_manipulate(tmp_path): result = runner.invoke( tested.app, [ + # fmt: off "manipulate", "--clip", - "--base-nrrd", - nrrd, - "--add", - nrrd, - "--subtract", - nrrd, - "--add", - nrrd, - "--subtract", - nrrd, - "--subtract", - nrrd, - "--output-path", - tmp_path / "manipulate.nrrd", + "--base-nrrd", nrrd, + "--add", nrrd, + "--subtract", nrrd, + "--add", nrrd, + "--subtract", nrrd, + "--subtract", nrrd, + "--output-path", tmp_path / "manipulate.nrrd", + # fmt: on ], catch_exceptions=False, ) @@ -184,16 +177,14 @@ def test_manipulate_clip(tmp_path): result = runner.invoke( tested.app, [ + # fmt: off "manipulate", "--clip", - "--base-nrrd", - nrrd, - "--subtract", - nrrd, - "--subtract", - nrrd, - "--output-path", - tmp_path / "manipulate.nrrd", + "--base-nrrd", nrrd, + "--subtract", nrrd, + "--subtract", nrrd, + "--output-path", tmp_path / "manipulate.nrrd", + # fmt: on ], catch_exceptions=False, ) @@ -205,15 +196,13 @@ def test_manipulate_clip(tmp_path): result = runner.invoke( tested.app, [ + # fmt: off "manipulate", - "--base-nrrd", - nrrd, - "--subtract", - nrrd, - "--subtract", - nrrd, - "--output-path", - tmp_path / "manipulate.nrrd", + "--base-nrrd", nrrd, + "--subtract", nrrd, + "--subtract", nrrd, + "--output-path", tmp_path / "manipulate.nrrd", + # fmt: on ], catch_exceptions=False, ) diff --git a/tests/app/test_mtype_densities.py b/tests/app/test_mtype_densities.py index 4927a80..4f16ae0 100644 --- a/tests/app/test_mtype_densities.py +++ b/tests/app/test_mtype_densities.py @@ -23,27 +23,23 @@ create_inhibitory_neuron_density, create_slicer_data, ) +from tests.utils import write_json def get_result_from_profiles(runner, td): return runner.invoke( tested.app, [ - "--log-output-path", - str(td), + # fmt: off + "--log-output-path", td, "create-from-profile", - "--annotation-path", - "annotation.nrrd", - "--hierarchy-path", - "hierarchy.json", - "--metadata-path", - "metadata.json", - "--direction-vectors-path", - "direction_vectors.nrrd", - "--mtypes-config-path", - "config.yaml", - "--output-dir", - "output_dir", + "--annotation-path", "annotation.nrrd", + "--hierarchy-path", "hierarchy.json", + "--metadata-path", "metadata.json", + "--direction-vectors-path", "direction_vectors.nrrd", + "--mtypes-config-path", "config.yaml", + "--output-dir", "output_dir", + # fmt: on ], ) @@ -56,10 +52,8 @@ def test_mtype_densities_from_profiles(tmp_path): data = create_slicer_data() data["annotation"].save_nrrd("annotation.nrrd") data["annotation"].with_data(data["direction_vectors"]).save_nrrd("direction_vectors.nrrd") - with open("metadata.json", "w", encoding="utf-8") as file_: - json.dump(data["metadata"], file_) - with open("hierarchy.json", "w", encoding="utf-8") as file_: - json.dump(data["hierarchy"], file_) + write_json("metadata.json", data["metadata"]) + write_json("hierarchy.json", data["hierarchy"]) with open("config.yaml", "w", encoding="utf-8") as file_: config = { "mtypeToProfileMapPath": str(DATA_PATH / "meta" / "mapping.tsv"), @@ -97,74 +91,53 @@ def get_result_from_probablity_map_(runner, td): return runner.invoke( tested.app, [ - "--log-output-path", - str(td), + # fmt: off + "--log-output-path", td, "create-from-probability-map", - "--annotation-path", - "annotation.nrrd", - "--hierarchy-path", - "hierarchy.json", - "--probability-map", - "probability_map01.csv", - "--probability-map", - "probability_map02.csv", - "--marker", - "pv", - "pv.nrrd", - "--marker", - "sst", - "sst.nrrd", - "--marker", - "vip", - "vip.nrrd", - "--marker", - "gad67", - "gad67.nrrd", - "--marker", - "approx_lamp5", - "approx_lamp5.nrrd", - "--synapse-class", - "EXC", - "--output-dir", - "output_dir", + "--annotation-path", "annotation.nrrd", + "--hierarchy-path", "hierarchy.json", + "--probability-map", "probability_map01.csv", + "--probability-map", "probability_map02.csv", + "--marker", "pv", "pv.nrrd", + "--marker", "sst", "sst.nrrd", + "--marker", "vip", "vip.nrrd", + "--marker", "gad67", "gad67.nrrd", + "--marker", "approx_lamp5", "approx_lamp5.nrrd", + "--synapse-class", "EXC", + "--output-dir", "output_dir", + # fmt: on ], ) -class Test_mtype_densities_from_probability_map: - def setup_method(self, method): - self.data = create_from_probability_map_data() - - def save_input_data_to_file(self): - self.data["annotation"].save_nrrd("annotation.nrrd") - with open("hierarchy.json", "w", encoding="utf-8") as file: - json.dump(self.data["hierarchy"], file) +def test_mtype_densities_from_probability_map(tmp_path): + data = create_from_probability_map_data() + runner = CliRunner() + with runner.isolated_filesystem(temp_dir=tmp_path) as td: + data["annotation"].save_nrrd("annotation.nrrd") + write_json("hierarchy.json", data["hierarchy"]) - self.data["probability_map01"].to_csv("probability_map01.csv", index=True) - self.data["probability_map02"].to_csv("probability_map02.csv", index=True) + data["probability_map01"].to_csv("probability_map01.csv", index=True) + data["probability_map02"].to_csv("probability_map02.csv", index=True) - for molecular_type, data in self.data["molecular_type_densities"].items(): + for molecular_type, raw_data in data["molecular_type_densities"].items(): VoxelData( - data, - voxel_dimensions=self.data["annotation"].voxel_dimensions, + raw_data, + voxel_dimensions=data["annotation"].voxel_dimensions, ).save_nrrd(f"{molecular_type}.nrrd") - def test_output(self, tmp_path): - runner = CliRunner() - with runner.isolated_filesystem(temp_dir=tmp_path) as td: - self.save_input_data_to_file() - result = get_result_from_probablity_map_(runner, td) - assert result.exit_code == 0 + result = get_result_from_probablity_map_(runner, td) + assert result.exit_code == 0 - BPbAC = VoxelData.load_nrrd(str(Path("output_dir") / "BP|bAC_EXC_densities.nrrd")) - assert BPbAC.raw.dtype == float - npt.assert_array_equal(BPbAC.voxel_dimensions, self.data["annotation"].voxel_dimensions) + BPbAC = VoxelData.load_nrrd(str(Path("output_dir") / "BP|bAC_EXC_densities.nrrd")) + assert BPbAC.raw.dtype == float + npt.assert_array_equal(BPbAC.voxel_dimensions, data["annotation"].voxel_dimensions) - with open(str(Path("output_dir") / "metadata.json"), "r") as file: - metadata = json.load(file) - assert "BP" in metadata["density_files"] - assert "bAC" in metadata["density_files"]["BP"] - assert "EXC" == metadata["synapse_class"] + with open(str(Path("output_dir") / "metadata.json"), "r") as file: + metadata = json.load(file) + assert "BP" in metadata["density_files"] + assert "bAC" in metadata["density_files"]["BP"] + assert "EXC" == metadata["synapse_class"] class Test_mtype_densities_from_composition: @@ -236,8 +209,7 @@ def hierarchy_path(self, class_tmpdir): }, ], } - with open(path, "w", encoding="utf-8") as jsonfile: - json.dump(hierarchy, jsonfile, indent=1, separators=(",", ": ")) + write_json(path, hierarchy) return path @pytest.fixture(scope="session") @@ -264,9 +236,7 @@ def metadata_path(self, class_tmpdir): "with_descendants": True, }, } - with open(path, "w", encoding="utf-8") as jsonfile: - json.dump(metadata, jsonfile, indent=1) - + write_json(path, metadata) return path @pytest.fixture(scope="session") @@ -403,20 +373,15 @@ def test_create_from_composition( result = runner.invoke( tested.create_from_composition, [ - "--annotation-path", - annotation_path, - "--hierarchy-path", - hierarchy_path, - "--metadata-path", - metadata_path, - "--excitatory-neuron-density-path", - density_path, - "--taxonomy-path", - taxonomy_path, - "--composition-path", - composition_path, - "--output-dir", - output_dir, + # fmt: off + "--annotation-path", annotation_path, + "--hierarchy-path", hierarchy_path, + "--metadata-path", metadata_path, + "--excitatory-neuron-density-path", density_path, + "--taxonomy-path", taxonomy_path, + "--composition-path", composition_path, + "--output-dir", output_dir, + # fmt: on ], ) diff --git a/tests/app/test_refined_inhibitory_neuron_densities.py b/tests/app/test_refined_inhibitory_neuron_densities.py index 0de8df8..fcdff6a 100644 --- a/tests/app/test_refined_inhibitory_neuron_densities.py +++ b/tests/app/test_refined_inhibitory_neuron_densities.py @@ -1,5 +1,4 @@ """test cell_densities""" -import json from pathlib import Path import numpy as np @@ -14,23 +13,20 @@ from tests.densities.test_refined_inhibitory_neuron_density import ( get_inhibitory_neuron_densities_data, ) +from tests.utils import write_json def _get_inhibitory_neuron_densities_result(runner, algorithm=None): args = [ + # fmt: off "inhibitory-neuron-densities", - "--hierarchy-path", - "hierarchy.json", - "--annotation-path", - "annotation.nrrd", - "--neuron-density-path", - "neuron_density.nrrd", - "--average-densities-path", - "average_densities.csv", - "--algorithm", - "keep-proportions", - "--output-dir", - "output_dir", + "--hierarchy-path", "hierarchy.json", + "--annotation-path", "annotation.nrrd", + "--neuron-density-path", "neuron_density.nrrd", + "--average-densities-path", "average_densities.csv", + "--algorithm", "keep-proportions", + "--output-dir", "output_dir", + # fmt: on ] if algorithm is not None: @@ -49,8 +45,7 @@ def _test(input_, algorithm=None): VoxelData(input_["neuron_density"], voxel_dimensions=voxel_dimensions).save_nrrd( "neuron_density.nrrd" ) - with open("hierarchy.json", "w") as file_: - json.dump(input_["hierarchy"], file_, indent=1, separators=(",", ": ")) + write_json("hierarchy.json", input_["hierarchy"]) input_["average_densities"]["brain_region"] = input_["average_densities"].index input_["average_densities"].to_csv("average_densities.csv", index=False) @@ -75,9 +70,8 @@ def _test(input_, algorithm=None): assert np.all(gad_data.raw >= subsum_density) # Test assertion in case of invalid hierarchy file - with open("hierarchy.json", "w", encoding="utf-8") as file_: - hierarchy = {"msg": [input_["hierarchy"], {}]} - json.dump(hierarchy, file_, indent=1, separators=(",", ": ")) + hierarchy = {"msg": [input_["hierarchy"], {}]} + write_json("hierarchy.json", hierarchy) result = _get_inhibitory_neuron_densities_result(runner, algorithm) assert "Unexpected JSON layout" in str(result.exception) diff --git a/tests/utils.py b/tests/utils.py new file mode 100644 index 0000000..0db39d8 --- /dev/null +++ b/tests/utils.py @@ -0,0 +1,7 @@ +import json + + +def write_json(filepath, data): + """Write json file.""" + with open(filepath, "w", encoding="utf-8") as fd: + json.dump(data, fd, indent=1, separators=(",", ": "))