Skip to content

Commit

Permalink
Added NaN Values to Test Data for NaN Removal in operations_tests.py (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
samtygier-stfc committed May 9, 2024
2 parents b44db12 + c712e06 commit 931065b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 49 deletions.
23 changes: 22 additions & 1 deletion scripts/operations_tests/operations_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
Expand Down Expand Up @@ -137,6 +138,10 @@ def run_tests(self):

def run_test(self, test_case):
image_stack = self.load_image_stack()

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")

Expand All @@ -158,6 +163,15 @@ def run_test(self, test_case):

TEST_CASE_RESULTS.append(test_case)

def add_nan(self, image_stack, fraction=0.001):
data = image_stack.data
total_elements = data.size
num_nans = int(total_elements * fraction)
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):
for operation, test_case_info in TEST_CASES.items():
print(f"Running tests for {operation}:")
Expand All @@ -171,7 +185,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")

Expand Down
77 changes: 29 additions & 48 deletions scripts/operations_tests/test_cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 931065b

Please sign in to comment.