From ba175b5ebf8b08536b0a28a5a50bff9fc1b843ba Mon Sep 17 00:00:00 2001 From: Sander Roet Date: Fri, 2 Feb 2024 17:12:11 +0100 Subject: [PATCH 1/8] Make filter warning template.py conditional --- src/pytom_tm/template.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/pytom_tm/template.py b/src/pytom_tm/template.py index db6bed66..d36296d7 100644 --- a/src/pytom_tm/template.py +++ b/src/pytom_tm/template.py @@ -37,8 +37,9 @@ def generate_template_from_map( ) 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)') + warning_text = (f"Filter resoltution is {'not specified' if filter_to_resolution is None else '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! From 5f990f1161e5d053c497b71a0f74b697764eab1b Mon Sep 17 00:00:00 2001 From: Sander Roet Date: Fri, 9 Feb 2024 15:37:12 +0100 Subject: [PATCH 2/8] allow default for ctf_params --- src/pytom_tm/template.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pytom_tm/template.py b/src/pytom_tm/template.py index d36296d7..d3fee3ba 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, From 52b1420abd9e1da96de833ce47683b71ca146adb Mon Sep 17 00:00:00 2001 From: Sander Roet Date: Fri, 9 Feb 2024 15:38:58 +0100 Subject: [PATCH 3/8] add test for logging --- tests/test_logging.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 tests/test_logging.py diff --git a/tests/test_logging.py b/tests/test_logging.py new file mode 100644 index 00000000..5b60e1ab --- /dev/null +++ b/tests/test_logging.py @@ -0,0 +1,28 @@ +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 missing filter resolution + with self.assertLogs(level='WARNING') as cm: + _ = generate_template_from_map(template, 1., 1.) + self.assertEqual(len(cm.output) == 1) + self.assertIn('Filter resolution', cm.output[0]) + self.assertIn(' not specified ',cm.output[0]) + self.assertNotIn(' too low ', cm.output[0]) + + # 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.assertNotIn(' not specified ',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) + From 5ff5cce47b4f91b4c69f8be27cf184d5427cc058 Mon Sep 17 00:00:00 2001 From: Sander Roet Date: Fri, 9 Feb 2024 15:42:05 +0100 Subject: [PATCH 4/8] use proper call to assertEqual --- tests/test_logging.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_logging.py b/tests/test_logging.py index 5b60e1ab..2277a8f1 100644 --- a/tests/test_logging.py +++ b/tests/test_logging.py @@ -9,7 +9,7 @@ def test_lowpass_resolution(self): # Test missing filter resolution with self.assertLogs(level='WARNING') as cm: _ = generate_template_from_map(template, 1., 1.) - self.assertEqual(len(cm.output) == 1) + self.assertEqual(len(cm.output), 1) self.assertIn('Filter resolution', cm.output[0]) self.assertIn(' not specified ',cm.output[0]) self.assertNotIn(' too low ', cm.output[0]) @@ -17,7 +17,7 @@ def test_lowpass_resolution(self): # 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.assertEqual(len(cm.output), 1) self.assertIn('Filter resolution', cm.output[0]) self.assertNotIn(' not specified ',cm.output[0]) self.assertIn(' too low ', cm.output[0]) From 56149043851d31b258b686380a49e00752c79dcb Mon Sep 17 00:00:00 2001 From: Sander Roet Date: Fri, 9 Feb 2024 15:43:37 +0100 Subject: [PATCH 5/8] this is why we test logging --- src/pytom_tm/template.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pytom_tm/template.py b/src/pytom_tm/template.py index d3fee3ba..3d695c9e 100644 --- a/src/pytom_tm/template.py +++ b/src/pytom_tm/template.py @@ -37,7 +37,7 @@ def generate_template_from_map( ) if filter_to_resolution is None or filter_to_resolution < (2 * output_spacing): - warning_text = (f"Filter resoltution is {'not specified' if filter_to_resolution is None else 'too low'}," + warning_text = (f"Filter resolution is {'not specified' if filter_to_resolution is None else 'too low'}," f" setting to {2 * output_spacing}A (2 * output voxel size)") logging.warning(warning_text) filter_to_resolution = 2 * output_spacing From ec3da8aa61203ef9846b418bd3bd1091209e7060 Mon Sep 17 00:00:00 2001 From: Sander Roet Date: Fri, 9 Feb 2024 15:45:41 +0100 Subject: [PATCH 6/8] no space after but a comma --- tests/test_logging.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_logging.py b/tests/test_logging.py index 2277a8f1..f1f56205 100644 --- a/tests/test_logging.py +++ b/tests/test_logging.py @@ -11,16 +11,16 @@ def test_lowpass_resolution(self): _ = generate_template_from_map(template, 1., 1.) self.assertEqual(len(cm.output), 1) self.assertIn('Filter resolution', cm.output[0]) - self.assertIn(' not specified ',cm.output[0]) - self.assertNotIn(' too low ', cm.output[0]) + self.assertIn(' not specified,',cm.output[0]) + self.assertNotIn(' too low,', cm.output[0]) # 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.assertNotIn(' not specified ',cm.output[0]) - self.assertIn(' too low ', cm.output[0]) + self.assertNotIn(' not specified,',cm.output[0]) + self.assertIn(' too low,', cm.output[0]) # Test working filter resolution with self.assertNoLogs(level='WARNING'): From e4482cf3eb7e5805778a83c350c38d731244b93f Mon Sep 17 00:00:00 2001 From: Sander Roet Date: Mon, 12 Feb 2024 15:45:27 +0100 Subject: [PATCH 7/8] restructure based on Marten's comments, only warn if overriding a set frequency --- src/pytom_tm/template.py | 5 ++++- tests/test_logging.py | 11 +---------- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/src/pytom_tm/template.py b/src/pytom_tm/template.py index 3d695c9e..7e638850 100644 --- a/src/pytom_tm/template.py +++ b/src/pytom_tm/template.py @@ -37,7 +37,10 @@ def generate_template_from_map( ) if filter_to_resolution is None or filter_to_resolution < (2 * output_spacing): - warning_text = (f"Filter resolution is {'not specified' if filter_to_resolution is None else 'too low'}," + # 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 diff --git a/tests/test_logging.py b/tests/test_logging.py index f1f56205..e2bbabae 100644 --- a/tests/test_logging.py +++ b/tests/test_logging.py @@ -6,21 +6,12 @@ class TestLogging(unittest.TestCase): def test_lowpass_resolution(self): template = np.zeros((13, 13, 13), dtype=np.float32) - # Test missing filter resolution - with self.assertLogs(level='WARNING') as cm: - _ = generate_template_from_map(template, 1., 1.) - self.assertEqual(len(cm.output), 1) - self.assertIn('Filter resolution', cm.output[0]) - self.assertIn(' not specified,',cm.output[0]) - self.assertNotIn(' too low,', cm.output[0]) - # 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.assertNotIn(' not specified,',cm.output[0]) - self.assertIn(' too low,', cm.output[0]) + self.assertIn('too low', cm.output[0]) # Test working filter resolution with self.assertNoLogs(level='WARNING'): From 9e83c705e6c89429088aae32b1b8fc024602aedc Mon Sep 17 00:00:00 2001 From: Sander Roet Date: Mon, 12 Feb 2024 16:04:28 +0100 Subject: [PATCH 8/8] Update src/pytom_tm/template.py Co-authored-by: Marten <58044494+McHaillet@users.noreply.github.com> --- src/pytom_tm/template.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pytom_tm/template.py b/src/pytom_tm/template.py index 7e638850..dbe4b448 100644 --- a/src/pytom_tm/template.py +++ b/src/pytom_tm/template.py @@ -36,7 +36,7 @@ def generate_template_from_map( mode='edge' ) - if filter_to_resolution is None or filter_to_resolution < (2 * output_spacing): + if filter_to_resolution is None: # Set to nyquist resolution filter_to_resolution = 2 * output_spacing elif filter_to_resolution < (2 * output_spacing):