From 1689f1a42bb79a19f9d97604d2ae1572e88f1d1e Mon Sep 17 00:00:00 2001 From: Mike Gevaert Date: Mon, 25 Mar 2024 14:37:19 +0100 Subject: [PATCH 1/5] line up app test arguments --- tests/app/test_cell_densities.py | 187 +++++++----------- tests/app/test_combination.py | 65 +++--- tests/app/test_mtype_densities.py | 89 +++------ ...est_refined_inhibitory_neuron_densities.py | 20 +- 4 files changed, 140 insertions(+), 221 deletions(-) diff --git a/tests/app/test_cell_densities.py b/tests/app/test_cell_densities.py index df2d5dc..acb358a 100644 --- a/tests/app/test_cell_densities.py +++ b/tests/app/test_cell_densities.py @@ -39,22 +39,6 @@ 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( @@ -77,7 +61,18 @@ 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") @@ -85,37 +80,30 @@ def test_cell_density(): # 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): + return + + +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() @@ -133,7 +121,7 @@ def test_glia_cell_densities(): ) with open("glia_proportions.json", "w", encoding="utf-8") as out: json.dump(input_["glia_proportions"], out) - result = _get_glia_cell_densities_result(runner) + result = runner.invoke(tested.app, args) assert result.exit_code == 0 neuron_density = VoxelData.load_nrrd("densities/neuron_density.nrrd") @@ -149,7 +137,7 @@ 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 "voxel_dimensions" in str(result.exception) # Check that an exception is thrown if the input cell density has negative values @@ -157,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) @@ -191,7 +171,7 @@ 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") @@ -208,7 +188,7 @@ def test_inhibitory_and_excitatory_neuron_densities(): 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 @@ -216,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") @@ -266,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) @@ -319,32 +289,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() @@ -379,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") @@ -404,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..b85a887 100644 --- a/tests/app/test_mtype_densities.py +++ b/tests/app/test_mtype_densities.py @@ -29,21 +29,16 @@ 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 ], ) @@ -97,36 +92,21 @@ 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 ], ) @@ -403,20 +383,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..b87bb13 100644 --- a/tests/app/test_refined_inhibitory_neuron_densities.py +++ b/tests/app/test_refined_inhibitory_neuron_densities.py @@ -18,19 +18,15 @@ 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: From c558465725f21da082b834d184ceb462b6f2450b Mon Sep 17 00:00:00 2001 From: Mike Gevaert Date: Mon, 25 Mar 2024 15:30:38 +0100 Subject: [PATCH 2/5] use a function to write json files --- tests/app/test_cell_densities.py | 24 ++++++++---------------- tests/app/test_mtype_densities.py | 10 ++++------ tests/utils.py | 6 ++++++ 3 files changed, 18 insertions(+), 22 deletions(-) create mode 100644 tests/utils.py diff --git a/tests/app/test_cell_densities.py b/tests/app/test_cell_densities.py index acb358a..43f553a 100644 --- a/tests/app/test_cell_densities.py +++ b/tests/app/test_cell_densities.py @@ -24,6 +24,7 @@ check_non_negative_values, get_invalid_region_names, ) +from tests.utils import write_json from tests.densities.test_fitting import get_fitting_input_data from tests.densities.test_glia_densities import get_glia_input_data from tests.densities.test_inhibitory_neuron_density import get_inhibitory_neuron_input_data @@ -84,10 +85,6 @@ def test_cell_density(): assert "voxel_dimensions" in str(result.exception) -def _get_glia_cell_densities_result(runner): - return - - def test_glia_cell_densities(): args = [ # fmt: off @@ -119,8 +116,9 @@ 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) + + write_json("glia_proportions.json", input_["glia_proportions"]) + result = runner.invoke(tested.app, args) assert result.exit_code == 0 @@ -264,8 +262,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" @@ -313,14 +310,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 = { diff --git a/tests/app/test_mtype_densities.py b/tests/app/test_mtype_densities.py index b85a887..3c26915 100644 --- a/tests/app/test_mtype_densities.py +++ b/tests/app/test_mtype_densities.py @@ -14,6 +14,7 @@ from voxcell import VoxelData # type: ignore import atlas_densities.app.mtype_densities as tested +from tests.utils import write_json from atlas_densities.exceptions import AtlasDensitiesError from tests.densities.test_mtype_densities_from_map import create_from_probability_map_data from tests.densities.test_mtype_densities_from_profiles import ( @@ -51,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"), @@ -117,8 +116,7 @@ def setup_method(self, method): 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) + write_json("hierarchy.json", self.data["hierarchy"]) self.data["probability_map01"].to_csv("probability_map01.csv", index=True) self.data["probability_map02"].to_csv("probability_map02.csv", index=True) diff --git a/tests/utils.py b/tests/utils.py new file mode 100644 index 0000000..b6a8871 --- /dev/null +++ b/tests/utils.py @@ -0,0 +1,6 @@ +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=(",", ": ")) From 31328107a2d376b47e347651e107e6d77eab2512 Mon Sep 17 00:00:00 2001 From: Mike Gevaert Date: Mon, 25 Mar 2024 15:35:08 +0100 Subject: [PATCH 3/5] `Test_mtype_densities_from_probability_map` doesn't need to be a class --- tests/app/test_mtype_densities.py | 51 ++++++++++++++----------------- 1 file changed, 23 insertions(+), 28 deletions(-) diff --git a/tests/app/test_mtype_densities.py b/tests/app/test_mtype_densities.py index 3c26915..430b380 100644 --- a/tests/app/test_mtype_densities.py +++ b/tests/app/test_mtype_densities.py @@ -110,39 +110,34 @@ def get_result_from_probablity_map_(runner, td): ) -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") - write_json("hierarchy.json", self.data["hierarchy"]) +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 - - 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) - - 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"] + 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, 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"] class Test_mtype_densities_from_composition: From 090f17a493c6e8e759960fe9ca574b705cd2f125 Mon Sep 17 00:00:00 2001 From: Mike Gevaert Date: Mon, 25 Mar 2024 16:02:48 +0100 Subject: [PATCH 4/5] last of the json.dump --- tests/app/test_cell_densities.py | 2 +- tests/app/test_mtype_densities.py | 9 +++------ tests/app/test_refined_inhibitory_neuron_densities.py | 10 ++++------ tests/utils.py | 1 + 4 files changed, 9 insertions(+), 13 deletions(-) diff --git a/tests/app/test_cell_densities.py b/tests/app/test_cell_densities.py index 43f553a..86ebf57 100644 --- a/tests/app/test_cell_densities.py +++ b/tests/app/test_cell_densities.py @@ -24,7 +24,6 @@ check_non_negative_values, get_invalid_region_names, ) -from tests.utils import write_json from tests.densities.test_fitting import get_fitting_input_data from tests.densities.test_glia_densities import get_glia_input_data from tests.densities.test_inhibitory_neuron_density import get_inhibitory_neuron_input_data @@ -34,6 +33,7 @@ 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" diff --git a/tests/app/test_mtype_densities.py b/tests/app/test_mtype_densities.py index 430b380..4f16ae0 100644 --- a/tests/app/test_mtype_densities.py +++ b/tests/app/test_mtype_densities.py @@ -14,7 +14,6 @@ from voxcell import VoxelData # type: ignore import atlas_densities.app.mtype_densities as tested -from tests.utils import write_json from atlas_densities.exceptions import AtlasDensitiesError from tests.densities.test_mtype_densities_from_map import create_from_probability_map_data from tests.densities.test_mtype_densities_from_profiles import ( @@ -24,6 +23,7 @@ create_inhibitory_neuron_density, create_slicer_data, ) +from tests.utils import write_json def get_result_from_profiles(runner, td): @@ -209,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") @@ -237,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") diff --git a/tests/app/test_refined_inhibitory_neuron_densities.py b/tests/app/test_refined_inhibitory_neuron_densities.py index b87bb13..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,6 +13,7 @@ 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): @@ -45,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) @@ -71,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 index b6a8871..0db39d8 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -1,5 +1,6 @@ import json + def write_json(filepath, data): """Write json file.""" with open(filepath, "w", encoding="utf-8") as fd: From 3d76a5a9e97c1d6888eb91fe90e812d9cb87aba3 Mon Sep 17 00:00:00 2001 From: Mike Gevaert Date: Tue, 26 Mar 2024 09:19:46 +0100 Subject: [PATCH 5/5] use simple assert for shapes --- tests/app/test_cell_densities.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/app/test_cell_densities.py b/tests/app/test_cell_densities.py index 86ebf57..40035a4 100644 --- a/tests/app/test_cell_densities.py +++ b/tests/app/test_cell_densities.py @@ -76,6 +76,7 @@ def test_cell_density(): 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 @@ -124,12 +125,12 @@ def test_glia_cell_densities(): 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( @@ -174,12 +175,12 @@ def test_inhibitory_and_excitatory_neuron_densities(): 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