Skip to content

Commit

Permalink
clean up test_mtype_densities_from_probability_map test
Browse files Browse the repository at this point in the history
  • Loading branch information
mgeplf committed Mar 22, 2024
1 parent 0bbf6b8 commit 0b9adec
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 69 deletions.
2 changes: 1 addition & 1 deletion atlas_densities/densities/fitting.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,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")
Expand Down
113 changes: 45 additions & 68 deletions tests/app/test_mtype_densities.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 0b9adec

Please sign in to comment.