Skip to content

Commit

Permalink
config_manager to be the global, and the rest of the code to access c…
Browse files Browse the repository at this point in the history
…onfig_manager.save_dir
  • Loading branch information
ashmeigh authored and samtygier-stfc committed Apr 10, 2024
1 parent eb11b5b commit a8cf122
Showing 1 changed file with 22 additions and 24 deletions.
46 changes: 22 additions & 24 deletions scripts/operations_tests/operations_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from collections import defaultdict
from dataclasses import dataclass
from datetime import datetime
from functools import cached_property
from pathlib import Path
from statistics import stdev
from collections.abc import Callable
Expand All @@ -26,34 +27,38 @@
print("Try: mamba install plotly")
exit(1)

sys.path.append(str(Path(__file__).parent.parent.parent))
sys.path.append(str(Path(__file__).resolve().parent.parent.parent))
from mantidimaging.core.io.filenames import FilenameGroup # noqa: E402
from mantidimaging.core.io.loader import loader # noqa: E402
from mantidimaging.core.operations.loader import load_filter_packages # noqa: E402

LOAD_SAMPLE = Path("C:/Users/44770/Downloads/mantidimaging-data-small "
"(2)/mantidimaging-data-small/ISIS/IMAT/IMAT00010675/Tomo/IMAT_Flower_Tomo_000000.tif")


class ConfigurationManager:
class ConfigManager:

def __init__(self):
self.save_dir = self._get_save_dir()
self._base_data_dir = Path.home() / "mantidimaging-data"

@cached_property
def save_dir(self):
dir_path = Path(os.getenv("MANTIDIMAGING_APPROVAL_TESTS_DIR", self._base_data_dir / "approval_tests"))
if not dir_path.exists():
print(f"Creating directory: {dir_path}")
dir_path.mkdir(parents=True, exist_ok=True)
return dir_path

@cached_property
def load_sample(self):
return self._base_data_dir / "ISIS" / "IMAT" / "IMAT00010675" / "Tomo" / "IMAT_Flower_Tomo_000000.tif"

def _get_save_dir(self):
path = os.getenv("MANTIDIMAGING_APPROVAL_TESTS_DIR")
if not path:
raise ValueError("Set MANTIDIMAGING_APPROVAL_TESTS_DIR for output path")
save_dir = Path(path)
save_dir.mkdir(parents=True, exist_ok=True)
return save_dir

config_manager = ConfigManager()

FILTERS = {f.filter_name: f for f in load_filter_packages()}
TEST_CASE_RESULTS: list[TestCase] = []
GIT_TOKEN = subprocess.check_output(["git", "describe"], encoding="utf_8").strip()
COMMIT_DATE = subprocess.check_output(["git", "log", "--pretty=format:%ai", "-n1"], encoding="utf_8").strip()
with open(Path(__file__).parent / "test_cases.json", encoding="UTF-8") as f:

with open(Path(__file__).resolve().parent / "test_cases.json", encoding="UTF-8") as f:
TEST_CASES = json.load(f)


Expand Down Expand Up @@ -182,11 +187,12 @@ def time_operation(image_stack, op_func, params):
def run_test(test_case):
image_stack = load_image_stack()
test_case.duration, new_image_stack = time_operation(image_stack, test_case.op_func, test_case.params)
file_name = config_manager.save_dir / (test_case.test_name + ".npz")

file_name = SAVE_DIR / (test_case.test_name + ".npz")
if file_name.is_file():
baseline_image_stack = load_post_operation_image_stack(file_name)
compare_image_stacks(baseline_image_stack, new_image_stack.data, test_case)

if test_case.status == "pass":
print(".", end="")
elif test_case.status == "fail":
Expand Down Expand Up @@ -216,7 +222,7 @@ def load_post_operation_image_stack(filepath):


def load_image_stack():
filename_group = FilenameGroup.from_file(Path(LOAD_SAMPLE))
filename_group = FilenameGroup.from_file(config_manager.load_sample)
filename_group.find_all_files()
image_stack = loader.load(filename_group=filename_group)
return image_stack
Expand Down Expand Up @@ -312,14 +318,6 @@ def create_plots():


def main():
global SAVE_DIR
try:
config_manager = ConfigurationManager()
SAVE_DIR = config_manager.save_dir
print(f"Save directory is set to: {SAVE_DIR}")
except ValueError as e:
print(e)
exit(1)
parser = argparse.ArgumentParser()
parser.add_argument("-m",
"--mode",
Expand Down

0 comments on commit a8cf122

Please sign in to comment.