From ca91fc55e11b06313d37a6ce8f00c8b01e36554e Mon Sep 17 00:00:00 2001 From: Mike Gevaert Date: Fri, 22 Mar 2024 13:49:52 +0100 Subject: [PATCH] clean up test_mtype_densities_from_probability_map test --- atlas_densities/densities/fitting.py | 2 +- tests/app/test_mtype_densities.py | 113 +++++++++++---------------- 2 files changed, 46 insertions(+), 69 deletions(-) diff --git a/atlas_densities/densities/fitting.py b/atlas_densities/densities/fitting.py index 6d1c961..25b9b0f 100644 --- a/atlas_densities/densities/fitting.py +++ b/atlas_densities/densities/fitting.py @@ -337,7 +337,7 @@ def compute_average_intensities( continue work.append(_helper(annotation, gene_marker_volumes, id_)) - res = Parallel(n_jobs=-2)(work) + res = Parallel()(work) densities = ( pd.DataFrame(list(it.chain(*res)), columns=["marker", "id", "voxel_count", "density"]) .set_index("id") diff --git a/tests/app/test_mtype_densities.py b/tests/app/test_mtype_densities.py index 4927a80..3b43dff 100644 --- a/tests/app/test_mtype_densities.py +++ b/tests/app/test_mtype_densities.py @@ -93,78 +93,55 @@ def test_mtype_densities_from_profiles(tmp_path): assert "neuron density file" in str(result.exception) -def get_result_from_probablity_map_(runner, td): - return runner.invoke( - tested.app, - [ - "--log-output-path", - str(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", - ], - ) - - -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: + td = Path(td) + data["annotation"].save_nrrd(td / "annotation.nrrd") + with open(td / "hierarchy.json", "w", encoding="utf-8") as file: + json.dump(data["hierarchy"], file) - 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(td / "probability_map01.csv", index=True) + data["probability_map02"].to_csv(td / "probability_map02.csv", index=True) - for molecular_type, data in self.data["molecular_type_densities"].items(): + for molecular_type, raw in data["molecular_type_densities"].items(): VoxelData( - data, - voxel_dimensions=self.data["annotation"].voxel_dimensions, - ).save_nrrd(f"{molecular_type}.nrrd") + raw, + voxel_dimensions=data["annotation"].voxel_dimensions, + ).save_nrrd(td / f"{molecular_type}.nrrd") + + result = runner.invoke( + tested.app, + [ + # fmt: off + "--log-output-path", str(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", + # fmt: on + ], + ) + assert result.exit_code == 0 - 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"] + BPbAC = VoxelData.load_nrrd(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(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: