Skip to content

Commit 17e2fa5

Browse files
committed
revert last approach in favor of using evaluate per dev meeting
1 parent ca53313 commit 17e2fa5

2 files changed

Lines changed: 36 additions & 16 deletions

File tree

src/aspire/covariance/covar2d.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -562,12 +562,15 @@ def filters_to_basis_mats(self):
562562
optimized_expand = callable(
563563
getattr(self.basis.__class__, "expand_radial_vec", None)
564564
)
565-
if optimized_expand and isinstance(self.src.filter_stack, CTFFilter):
565+
if optimized_expand and self.src.filter_stack.radial:
566566
logger.info(
567-
"Found all filters are CTF, and `basis.expand_radial_vec` available using, bulk basis mat eval"
567+
"Found radial filter stack and `basis.expand_radial_vec` available."
568+
" Using bulk basis mat eval."
568569
)
569-
return self._ctf_filters_to_basis_mats()
570+
return self._filter_stack_to_basis_mats()
570571
else:
572+
# Note, can come back and optmize the filter eval to bulk, just not radial
573+
# For now use legacy path.
571574
logger.info("Using sequential basis mat eval")
572575
return self._filters_to_basis_mats()
573576

@@ -585,20 +588,18 @@ def _filters_to_basis_mats(self):
585588
]
586589
return basis_mats
587590

588-
def _ctf_filters_to_basis_mats(self):
589-
# lol
590-
logger.info("Extracting CTF filter parameters and generating eval points")
591-
params = self.src.filter_stack._ctf_params()
592-
593-
logger.info("Computing CTF filters at eval points")
591+
# todo, either rename _radial_filters_to_basis_mats or handle none radial
592+
# same remark as `filters_to_basis_mats`
593+
def _filter_stack_to_basis_mats(self):
594+
logger.info("Generating filter eval points")
594595
_filter_pts = self.basis._filter_pts
595596
# if we have many filters, might be worth trip to GPU
596597
if len(self.src.filter_stack) >= 2048:
597598
params = xp.asarray(params)
598599
_filter_pts = xp.asarray(_filter_pts)
599600

600-
_filter_vals = CTFFilter.ctf_formula(
601-
_filter_pts, self.src.pixel_size, *(params.T)
601+
_filter_vals = self.src.filter_stack.evaluate(
602+
_filter_pts, pixel_size=self.src.pixel_size
602603
)
603604

604605
logger.info("Computing basis radial expansion")

src/aspire/operators/filters.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,16 @@ def evaluate(self, omega, **kwargs):
8989
h = self._evaluate(omega, **kwargs)
9090

9191
if self.radial:
92-
h = np.take(h, idx)
92+
# The reshape and take axis gynmastics work to provide the
93+
# legacy idx taking functionality for both singleton and
94+
# stack cases.
95+
# reshape (stack, vals)
96+
h = h.reshape(len(self), -1)
97+
# keep stack, take along vals axis
98+
h = np.take(h, idx, axis=-1)
99+
# squeeze off stack dim from singleton case (preserves legacy behavior)
100+
if h.shape[0] == 1: # avoid error when len(h)>1
101+
h = np.squeeze(h, axis=0)
93102

94103
return h
95104

@@ -158,7 +167,12 @@ def __len__(self):
158167
return 1
159168

160169
def _ctf_params(self):
161-
raise NotImplementedError(f"Not implemented for {self.__class__.__name__}")
170+
"""
171+
Return n_filters-by-n_param array from prior filter.
172+
"""
173+
raise NotImplementedError(
174+
f"_ctf_params not implemented for {self.__class__.__name__}"
175+
)
162176

163177

164178
class DualFilter(Filter):
@@ -329,7 +343,7 @@ def _init_size(self):
329343
def _evaluate(self, omega, **kwargs):
330344
res = 1
331345
for c in self._components:
332-
res *= c.evaluate(omega, **kwargs)
346+
res = res * c.evaluate(omega, **kwargs)
333347
return res
334348

335349
def __len__(self):
@@ -341,8 +355,13 @@ def _ctf_params(self):
341355
342356
Raises error if multiple or none found.
343357
"""
344-
_params = [getattr(c, "_ctf_params", None) for c in self._components]
345-
_params = list(filter(_params, None))
358+
_params = []
359+
for c in self._components:
360+
try:
361+
_params.append(c._ctf_params())
362+
except NotImplementedError as e:
363+
pass
364+
346365
if len(_params) > 1:
347366
raise RuntimeError("Multiple filters with CTF parameters found.")
348367
elif len(_params) == 0:

0 commit comments

Comments
 (0)