Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

calculate spectrum whitening filter only in the search region #81

Merged
merged 3 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/pytom_tm/tmjob.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,17 @@ def __init__(
self.ctf_data = ctf_data
self.whiten_spectrum = whiten_spectrum
self.whitening_filter = self.output_dir.joinpath(f'{self.tomo_id}_whitening_filter.npy')
if self.whiten_spectrum and not self.whitening_filter.exists():
if self.whiten_spectrum:
logging.info('Estimating whitening filter...')
weights = 1 / np.sqrt(power_spectrum_profile(read_mrc(self.tomogram)))
weights = 1 / np.sqrt(
power_spectrum_profile(
read_mrc(self.tomogram)[
self.search_origin[0]: self.search_origin[0] + self.search_size[0],
self.search_origin[1]: self.search_origin[1] + self.search_size[1],
self.search_origin[2]: self.search_origin[2] + self.search_size[2]
]
)
)
weights /= weights.max() # scale to 1
np.save(self.whitening_filter, weights)

Expand Down
12 changes: 12 additions & 0 deletions tests/test_tmjob.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,18 @@ def test_tm_job_weighting_options(self):
score, angle = job.start_job(0, return_volumes=True)
self.assertEqual(score.shape, job.tomo_shape, msg='TMJob with only whitening filter failed')

# load the whitening filter from previous job to compare against
whitening_filter = np.load(TEST_WHITENING_FILTER)
job = TMJob('0', 10, TEST_TOMOGRAM, TEST_TEMPLATE, TEST_MASK, TEST_DATA_DIR,
angle_increment='90.00', voxel_size=1., whiten_spectrum=True, search_y=[10, 90])
new_whitening_filter = np.load(TEST_WHITENING_FILTER)
self.assertNotEqual(whitening_filter.shape, new_whitening_filter.shape,
msg='After reducing the search region along the largest dimension the whitening filter '
'should have less sampling points')
self.assertEqual(new_whitening_filter.shape, (max(job.search_size) // 2 + 1, ),
msg='The whitening filter does not have the expected size, it should be equal (x // 2) + 1, '
'where x is the largest dimension of the search box.')

# TMJob with none of these weighting options is tested in all other runs in this file.

def test_custom_angular_search(self):
Expand Down