Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tathey1 committed Jan 3, 2024
1 parent ed9df83 commit 135606d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 18 deletions.
15 changes: 8 additions & 7 deletions brainlit/BrainLine/apply_ilastik.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class ApplyIlastik:
Attributes:
ilastk_path (str): Path to ilastik executable.
project_path (str): Path to ilastik project.
brains_path (str): Path to directory that contains brain samples subdirectories.
brains_path (Path): Path to directory that contains brain samples subdirectories.
brains (list): List of brain sample names.
"""
Expand All @@ -46,7 +46,7 @@ def __init__(
):
self.ilastik_path = ilastik_path
self.project_path = project_path
self.brains_path = brains_path
self.brains_path = Path(brains_path)
self.brains = brains

def _apply_ilastik(self, fnames):
Expand Down Expand Up @@ -76,7 +76,7 @@ def process_subvols(self, dset: str = "val", ncpu: int = 6):
brain_name = "r2"
else:
brain_name = brain
path = f"{self.brains_path}brain{brain_name}/{dset}/"
path = self.brains_path / f"brain{brain_name}" / dset

items_total += _find_sample_names(path, dset="", add_dir=True)
print(f"Applying ilastik to {items_total}")
Expand All @@ -102,17 +102,18 @@ def move_results(self):
# else:
# brain_name = brain

brain_dir = f"{self.brains_path}brain{brain}/val/"
results_dir = brain_dir + "results" + str(date.today()) + "/"
brain_dir = self.brains_path / f"brain{brain}" / "val"
results_dir = brain_dir / f"results{date.today()}"

if not os.path.exists(results_dir):
print(f"Creating directory: {results_dir}")
os.makedirs(results_dir)

items = _find_sample_names(brain_dir, dset="", add_dir=False)
for item in items:
result_path = brain_dir + item[:-3] + "_Probabilities.h5"
shutil.move(result_path, results_dir + item[:-3] + "_Probabilities.h5")
prob_fname = f"{item[:-3]}_Probabilities.h5"
result_path = brain_dir / prob_fname
shutil.move(result_path, results_dir / prob_fname)


def plot_results(
Expand Down
29 changes: 18 additions & 11 deletions brainlit/BrainLine/tests/test_apply_ilastik.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import pytest
import matplotlib.pyplot as plt
from pathlib import Path
from datetime import date


def test_ApplyIlastik():
Expand Down Expand Up @@ -46,19 +47,9 @@ def axon_data_dir(tmp_path_factory):
return data_dir


def test_move_results(axon_data_dir):
data_dir_str = str(axon_data_dir)
apl = ApplyIlastik(
ilastik_path="test",
project_path="test",
brains_path=data_dir_str,
brains=["test"],
)
apl.move_results()


def test_plot_results_axon(axon_data_dir):
data_dir_str = str(axon_data_dir)

test_max_fscore, test_best_threshold = plot_results(
data_dir=data_dir_str,
brain_ids=["test"],
Expand Down Expand Up @@ -158,3 +149,19 @@ def test_ApplyIlastik_LargeImage():
ilastik_path="", ilastik_project="", ncpu=1, data_file=data_file
)
# Sample data is there but file path in data json is specific to thomastathey


def test_move_results(axon_data_dir):
data_dir_str = str(axon_data_dir)

apl = ApplyIlastik(
ilastik_path="test",
project_path="test",
brains_path=data_dir_str,
brains=["test"],
)

apl.move_results()

newdir = apl.brains_path / f"braintest/val/results{date.today()}"
assert len(os.listdir(newdir)) == 1
23 changes: 23 additions & 0 deletions brainlit/BrainLine/tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from cloudvolume import CloudVolume
from pathlib import Path
import os
import json
from skimage import io


@pytest.fixture(scope="session")
Expand Down Expand Up @@ -193,3 +195,24 @@ def test_fold():
test_fold = util._fold(img)

assert_array_equal(true_fold, test_fold)


def test_dir_to_atlas_pts(tmp_path):
json_dir = tmp_path / "json_data"
json_dir.mkdir()

json_data = [{"point": [-1, 0, 0]}, {"point": [1, 1, 1]}, {"point": [2, 1, 1]}]
with open(json_dir / "json1.json", "w") as f:
json.dump(json_data, f)

json_data = [{"point": [0, 0, 0]}, {"point": [1, 2, 1]}]
with open(json_dir / "json2.json", "w") as f:
json.dump(json_data, f)

atlas_file = tmp_path / "atlas.tif"
atlas_im = np.zeros((3, 3, 3), dtype="uint16")
atlas_im[1:, 1:, 1:] = 1
io.imsave(atlas_file, atlas_im)

outname = tmp_path / "filtered.txt"
util.dir_to_atlas_pts(dir=json_dir, outname=outname, atlas_file=atlas_file)

0 comments on commit 135606d

Please sign in to comment.