From 3f963fc9bc56c0a62a08a8e6992382b5e15ff41b Mon Sep 17 00:00:00 2001 From: Sander Roet Date: Mon, 12 Feb 2024 16:11:59 +0100 Subject: [PATCH] Template.py: make filter warnings conditional and allow for no ctf_params (#98) * Make filter warning template.py conditional * allow default for ctf_params * add test for logging * use proper call to assertEqual * this is why we test logging * no space after but a comma * restructure based on Marten's comments, only warn if overriding a set frequency * Update src/pytom_tm/template.py Co-authored-by: Marten <58044494+McHaillet@users.noreply.github.com> --------- Co-authored-by: Marten <58044494+McHaillet@users.noreply.github.com> --- src/pytom_tm/template.py | 12 ++++++++---- tests/test_logging.py | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 tests/test_logging.py diff --git a/src/pytom_tm/template.py b/src/pytom_tm/template.py index db6bed66..dbe4b448 100644 --- a/src/pytom_tm/template.py +++ b/src/pytom_tm/template.py @@ -20,7 +20,7 @@ def generate_template_from_map( input_map: npt.NDArray[float], input_spacing: float, output_spacing: float, - ctf_params: dict, + ctf_params: Optional[dict] = None, center: bool = False, filter_to_resolution: Optional[float] = None, output_box_size: Optional[int] = None, @@ -36,9 +36,13 @@ def generate_template_from_map( mode='edge' ) - if filter_to_resolution is None or filter_to_resolution < (2 * output_spacing): - logging.warning(f'Filter resolution is either not specified (in which case you can ignore this warning) ' - f'or too low, changing to {2 * output_spacing}A (2 * output voxel size)') + if filter_to_resolution is None: + # Set to nyquist resolution + filter_to_resolution = 2 * output_spacing + elif filter_to_resolution < (2 * output_spacing): + warning_text = (f"Filter resolution is too low," + f" setting to {2 * output_spacing}A (2 * output voxel size)") + logging.warning(warning_text) filter_to_resolution = 2 * output_spacing # extend volume to the desired output size before applying convolutions! diff --git a/tests/test_logging.py b/tests/test_logging.py new file mode 100644 index 00000000..e2bbabae --- /dev/null +++ b/tests/test_logging.py @@ -0,0 +1,19 @@ +import unittest +import numpy as np +from pytom_tm.template import generate_template_from_map + +class TestLogging(unittest.TestCase): + def test_lowpass_resolution(self): + template = np.zeros((13, 13, 13), dtype=np.float32) + + # Test too low filter resolution + with self.assertLogs(level='WARNING') as cm: + _ = generate_template_from_map(template, 1., 1., filter_to_resolution=1.5) + self.assertEqual(len(cm.output), 1) + self.assertIn('Filter resolution', cm.output[0]) + self.assertIn('too low', cm.output[0]) + + # Test working filter resolution + with self.assertNoLogs(level='WARNING'): + _ = generate_template_from_map(template, 1., 1., filter_to_resolution=2.5) +