From c5993c32511c3575bd76bdd17ed498556f19b782 Mon Sep 17 00:00:00 2001 From: ashmeigh Date: Thu, 25 Apr 2024 09:53:53 +0100 Subject: [PATCH 1/3] added NAN function --- scripts/operations_tests/operations_tests.py | 11 +++ scripts/operations_tests/test_cases.json | 77 ++++++++------------ 2 files changed, 40 insertions(+), 48 deletions(-) diff --git a/scripts/operations_tests/operations_tests.py b/scripts/operations_tests/operations_tests.py index e005a16eded..fa2b75d282f 100644 --- a/scripts/operations_tests/operations_tests.py +++ b/scripts/operations_tests/operations_tests.py @@ -140,6 +140,9 @@ def run_test(self, test_case): test_case.duration, new_image_stack = self.time_operation(image_stack, test_case.op_func, test_case.params) file_name = config_manager.save_dir / (test_case.test_name + ".npz") + if test_case.params.get('pre_run_step') == 'add_nan': + image_stack = self.add_nan(image_stack, fraction=0.1) + if file_name.is_file(): baseline_image_stack = self.load_post_operation_image_stack(file_name) self.compare_image_stacks(baseline_image_stack, new_image_stack.data, test_case) @@ -158,6 +161,14 @@ def run_test(self, test_case): TEST_CASE_RESULTS.append(test_case) + def add_nan(self, image_stack, fraction=0.1): + np.random.Generator(0) + total_elements = image_stack.size + num_nans = int(total_elements * fraction) + nan_indices = np.random.Generator(total_elements, num_nans, replace=False) + image_stack.ravel()[nan_indices] = np.nan + return image_stack + def compare_mode(self): for operation, test_case_info in TEST_CASES.items(): print(f"Running tests for {operation}:") diff --git a/scripts/operations_tests/test_cases.json b/scripts/operations_tests/test_cases.json index 6267143bbdc..aff100a0fcf 100644 --- a/scripts/operations_tests/test_cases.json +++ b/scripts/operations_tests/test_cases.json @@ -510,27 +510,37 @@ ] }, "NaN Removal": { - "params": { - "mode_value": "Constant", - "replace_value": null - }, - "source_data": "flower128", - "cases": [ - { - "test_name": "replace_with_constant", - "params": { - "mode_value": "Constant", - "replace_value": 0 - } + "params": { + "mode_value": "Constant", + "replace_value": 0 }, - { - "test_name": "replace_with_median", - "params": { - "mode_value": "Median" + "source_data": "flower128", + "cases": [ + { + "pre_run_step": "add_nan", + "test_name": "nan_removal_constant_default", + "params": { + "mode_value": "Constant", + "replace_value": 0 + } + }, + { + "pre_run_step": "add_nan", + "test_name": "nan_removal_median", + "params": { + "mode_value": "Median" + } + }, + { + "pre_run_step": "add_nan", + "test_name": "nan_removal_constant_specific", + "params": { + "mode_value": "Constant", + "replace_value": 100 + } } - } - ] - }, + ] + }, "Rebin": { "params": { "rebin_param": 0.5, @@ -632,35 +642,6 @@ } ] }, - "NaN Removal": { - "params": { - "mode_value": "Constant", - "replace_value": 0 - }, - "source_data": "flower128", - "cases": [ - { - "test_name": "nan_removal_constant_default", - "params": { - "mode_value": "Constant", - "replace_value": 0 - } - }, - { - "test_name": "nan_removal_median", - "params": { - "mode_value": "Median" - } - }, - { - "test_name": "nan_removal_constant_specific", - "params": { - "mode_value": "Constant", - "replace_value": 100 - } - } - ] - }, "Circular Mask": { "params": { "circular_mask_ratio": 0.95, From d7e819271c7c6c00efe37e872833b1194db26de7 Mon Sep 17 00:00:00 2001 From: ashmeigh Date: Tue, 7 May 2024 06:56:08 +0100 Subject: [PATCH 2/3] pre_run_step added as an attribute in TestCase and set in compare_mode() --- scripts/operations_tests/operations_tests.py | 27 +++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/scripts/operations_tests/operations_tests.py b/scripts/operations_tests/operations_tests.py index fa2b75d282f..4fc4c932121 100644 --- a/scripts/operations_tests/operations_tests.py +++ b/scripts/operations_tests/operations_tests.py @@ -83,6 +83,7 @@ class TestCase: sub_test_name: str test_number: int params: dict + pre_run_step: str op_func: Callable duration: float = 0.0 message: str = "" @@ -140,8 +141,8 @@ def run_test(self, test_case): test_case.duration, new_image_stack = self.time_operation(image_stack, test_case.op_func, test_case.params) file_name = config_manager.save_dir / (test_case.test_name + ".npz") - if test_case.params.get('pre_run_step') == 'add_nan': - image_stack = self.add_nan(image_stack, fraction=0.1) + if test_case.pre_run_step == 'add_nan': + self.image_stack = self.add_nan(image_stack, fraction=0.1) if file_name.is_file(): baseline_image_stack = self.load_post_operation_image_stack(file_name) @@ -162,11 +163,16 @@ def run_test(self, test_case): TEST_CASE_RESULTS.append(test_case) def add_nan(self, image_stack, fraction=0.1): - np.random.Generator(0) - total_elements = image_stack.size + data = image_stack.data + total_elements = data.size num_nans = int(total_elements * fraction) - nan_indices = np.random.Generator(total_elements, num_nans, replace=False) - image_stack.ravel()[nan_indices] = np.nan + + rng = np.random.default_rng(0) + + nan_indices = rng.choice(total_elements, num_nans, replace=False) + + data.ravel()[nan_indices] = np.nan + return image_stack def compare_mode(self): @@ -182,7 +188,14 @@ def compare_mode(self): params = {k: process_params(v) for k, v in params.items()} op_class = FILTERS[operation] op_func = op_class.filter_func - test_case = TestCase(operation, test_name, sub_test_name, test_number, params, op_func) + pre_run_step = case.get("pre_run_step", {}) + test_case = TestCase(operation=operation, + test_name=test_name, + sub_test_name=sub_test_name, + test_number=test_number, + params=params, + pre_run_step=pre_run_step, + op_func=op_func) self.run_test(test_case) print("\n") From c712e065acd81f9605cf675be4f705fcc3ca28e6 Mon Sep 17 00:00:00 2001 From: ashmeigh Date: Thu, 9 May 2024 12:07:31 +0100 Subject: [PATCH 3/3] spead out nans and added nans before operation --- scripts/operations_tests/operations_tests.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/scripts/operations_tests/operations_tests.py b/scripts/operations_tests/operations_tests.py index 4fc4c932121..e9d53382410 100644 --- a/scripts/operations_tests/operations_tests.py +++ b/scripts/operations_tests/operations_tests.py @@ -138,12 +138,13 @@ def run_tests(self): def run_test(self, test_case): image_stack = self.load_image_stack() - test_case.duration, new_image_stack = self.time_operation(image_stack, test_case.op_func, test_case.params) - file_name = config_manager.save_dir / (test_case.test_name + ".npz") if test_case.pre_run_step == 'add_nan': self.image_stack = self.add_nan(image_stack, fraction=0.1) + test_case.duration, new_image_stack = self.time_operation(image_stack, test_case.op_func, test_case.params) + file_name = config_manager.save_dir / (test_case.test_name + ".npz") + if file_name.is_file(): baseline_image_stack = self.load_post_operation_image_stack(file_name) self.compare_image_stacks(baseline_image_stack, new_image_stack.data, test_case) @@ -162,17 +163,13 @@ def run_test(self, test_case): TEST_CASE_RESULTS.append(test_case) - def add_nan(self, image_stack, fraction=0.1): + def add_nan(self, image_stack, fraction=0.001): data = image_stack.data total_elements = data.size num_nans = int(total_elements * fraction) - - rng = np.random.default_rng(0) - - nan_indices = rng.choice(total_elements, num_nans, replace=False) - + step_size = total_elements // num_nans + nan_indices = np.arange(0, total_elements, step_size)[:num_nans] data.ravel()[nan_indices] = np.nan - return image_stack def compare_mode(self):