Skip to content

Commit 174cd3c

Browse files
committed
cleanup some filter eval concerns
1 parent 6052ad7 commit 174cd3c

3 files changed

Lines changed: 16 additions & 11 deletions

File tree

src/aspire/operators/filters.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,18 @@ def evaluate_src_filters_on_grid(src, indices=None):
3131
grid2d = grid_2d(src.L, indexing="yx", dtype=src.dtype)
3232
omega = np.pi * np.vstack((grid2d["x"].flatten(), grid2d["y"].flatten()))
3333

34-
# xxx filter opt (eval in bulk instead of loop here), remove branch
3534
# Initialize h as ones to mimic an IdentityFilter when src.filter_stack is None.
3635
h = np.ones((omega.shape[-1], len(indices)), dtype=src.dtype)
36+
#### XXX I believe this might be what Tony reported ^
37+
3738
if src.filter_stack is not None:
38-
for i, filt in enumerate(src.filter_stack):
39+
# Evaluate all filters in bulk
40+
filter_stack_values = src.filter_stack.evaluate(
41+
omega, pixel_size=src.pixel_size
42+
)
43+
for i, filter_values in enumerate(filter_stack_values):
3944
idx_k = np.where(src.filter_indices[indices] == i)[0]
4045
if len(idx_k) > 0:
41-
filter_values = filt.evaluate(omega, pixel_size=src.pixel_size)
4246
# convert filter_values row vector to column vector and tile broadcast
4347
filter_values = filter_values.reshape(-1, 1)
4448
h[:, idx_k] = np.tile(filter_values, len(idx_k))

src/aspire/source/image.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,7 @@ def _apply_filters(
792792
return im
793793

794794
# else evaluate filters
795-
# XXXX broadcast filter eval
795+
# TODO broadcast filter eval
796796
for i, filt in enumerate(filters):
797797
idx_k = np.where(indices == i)[0]
798798
if len(idx_k) > 0:
@@ -876,7 +876,6 @@ def downsample(self, L, zero_nyquist=True, centered_fft=True):
876876
)
877877
)
878878

879-
# XXXX sigh
880879
ds_factor = self.L / L
881880
if self.filter_stack is not None:
882881
self.filter_stack = self.filter_stack.scale(ds_factor)
@@ -1004,7 +1003,7 @@ def legacy_whiten(self, noise_response=None, delta=None, batch_size=512):
10041003
if delta is None:
10051004
delta = np.finfo(np.float32).eps
10061005

1007-
# # XXX This "should be better" but totally breaks things.
1006+
# # TODO This "should be better" but totally breaks things.
10081007
# # First guess would be to check the strange normalization.
10091008
# # Can't fix everything at once.
10101009
# logger.info(f"Extending filter stack by legacy whitening Filter")
@@ -1027,7 +1026,6 @@ def phase_flip(self):
10271026
logger.info("Perform phase flip on source object")
10281027

10291028
if self.filter_stack is not None:
1030-
# XXXX
10311029
unique_xforms = FilterXform(self.filter_stack.sign)
10321030

10331031
logger.info("Adding Phase Flip Xform to end of generation pipeline")
@@ -1811,9 +1809,11 @@ def __init__(self, src, indices, memory=None):
18111809
_unq, _inv = np.unique(_filter_indices, return_inverse=True)
18121810
# Repack filter_stack
18131811
self.filter_indices = _inv
1814-
self.filter_stack = copy.copy(
1815-
src.filter_stack[_unq]
1816-
) # xxx, this might just work by slicing...
1812+
# This would work by slicing with current code,
1813+
# but if future code mutated the filter objects, that would be a problem.
1814+
# Copy for safety/intent.
1815+
# Deep copy may be required if future code mutates underlying objects.
1816+
self.filter_stack = copy.copy(src.filter_stack[_unq])
18171817
else:
18181818
# Pass through the None case
18191819
self.filter_stack = src.filter_stack

src/aspire/source/simulation.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,8 @@ def _populate_ctf_metadata(self, filter_indices):
228228
]
229229

230230
# Unpack the `filter_stack` params across images using `filter_indices` mapping
231-
# Note this does not include the B factor term (unique to ASPIRE?,xxx should we add to star if used?)
231+
# Note this does not include the B factor term (hence the truncation)
232+
# B factor term looks unique to ASPIRE, should we add to star if used?
232233
filter_stack_params = self.filter_stack._ctf_params()[
233234
:, :6
234235
] # params per filter

0 commit comments

Comments
 (0)