Skip to content

Commit

Permalink
Template.py: make filter warnings conditional and allow for no ctf_pa…
Browse files Browse the repository at this point in the history
…rams (#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 <[email protected]>

---------

Co-authored-by: Marten <[email protected]>
  • Loading branch information
sroet and McHaillet authored Feb 12, 2024
1 parent bc1fced commit 3f963fc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/pytom_tm/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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!
Expand Down
19 changes: 19 additions & 0 deletions tests/test_logging.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 3f963fc

Please sign in to comment.