Skip to content

Commit 8cba017

Browse files
committed
continue filter_basis_mat cleanup
1 parent 625606a commit 8cba017

2 files changed

Lines changed: 36 additions & 47 deletions

File tree

src/aspire/basis/fle_2d.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -780,16 +780,15 @@ def _radial_convolve_weights(self, b):
780780

781781
return a
782782

783-
# def filter_to_basis_mat(self, f, **kwargs):
784-
# """
785-
# See `SteerableBasis2D.filter_stack_to_basis_mats`.
786-
# """
787-
# if len(f) != 1:
788-
# raise RuntimeError("Unexpected filter length.")
789-
# return self._filter_stack_to_basis_mats(f, **kwargs)[0]
790-
791-
# XXX TODO, convert to _filter_stack_to_basis_mats via broadcasting.
792783
def filter_to_basis_mat(self, f, **kwargs):
784+
"""
785+
See `SteerableBasis2D.filter_stack_to_basis_mats`.
786+
"""
787+
if len(f) != 1:
788+
raise RuntimeError("Unexpected filter length.")
789+
return self._filter_stack_to_basis_mats(f, **kwargs)
790+
791+
def _filter_stack_to_basis_mats(self, f, **kwargs):
793792
"""
794793
See `SteerableBasis2D.filter_to_basis_mat`.
795794
@@ -840,20 +839,28 @@ def filter_to_basis_mat(self, f, **kwargs):
840839

841840
h_vals2d = (
842841
xp.asarray(h_fun(omega, pixel_size=pixel_size))
843-
.reshape(n_k, n_theta)
842+
.reshape(len(f), n_k, n_theta)
844843
.astype(self.dtype, copy=False)
845844
)
846-
h_vals = xp.sum(h_vals2d, axis=1) / n_theta
845+
h_vals = xp.sum(h_vals2d, axis=-1) / n_theta
847846

848-
h_basis = xp.zeros(self.count, dtype=self.dtype)
849-
# For now we just need to handle 1D (stack of one ctf)
847+
h_basis = xp.zeros((len(f), self.count), dtype=self.dtype)
848+
# shape gymnastics to get a broadcast with csr A3
849+
h_vals = h_vals.T
850850
for j in range(self.ell_p_max + 1):
851-
h_basis[self.idx_list[j]] = self.A3[j] @ h_vals
851+
h_basis[:, self.idx_list[j]] = (self.A3[j] @ h_vals).T
852852

853853
# Convert from internal FLE ordering to FB convention
854-
h_basis = h_basis[self._fle_to_fb_indices]
854+
h_basis = h_basis[:, self._fle_to_fb_indices]
855+
# who needs this as a list?
856+
857+
coefs = xp.asnumpy(h_basis)
858+
if len(coefs) > 1:
859+
coefs = [DiagMatrix(c) for c in coefs]
860+
else:
861+
coefs = DiagMatrix(coefs.flatten())
855862

856-
return DiagMatrix(xp.asnumpy(h_basis))
863+
return coefs
857864

858865
def expand_radial_vec(self, radial_vec, **kwargs):
859866
"""

src/aspire/covariance/covar2d.py

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -567,40 +567,22 @@ def filters_to_basis_mats(self):
567567
getattr(self.basis.__class__, "expand_radial_vec", None)
568568
)
569569

570+
# Are the filters radial?
571+
if self.src.filter_stack.radial:
572+
logger.info("Found radial filter stack.")
573+
else:
574+
logger.info("Found non-radial filter stack.")
575+
570576
if optimized_expand and self.src.filter_stack.radial:
571-
logger.info(
572-
"Found radial filter stack and `basis.expand_radial_vec` available."
573-
" Using bulk basis mat eval."
574-
)
577+
logger.info("Using optimized `basis.expand_radial_vec`.")
575578
return self._radial_filter_stack_to_basis_mats()
576579
else:
577-
# Note, can come back and optmize the filter eval to bulk, just not radial
578-
# For now use legacy path.
579-
logger.info("Using sequential basis mat eval")
580-
return self._filters_to_basis_mats()
581-
582-
def _filters_to_basis_mats(self):
583-
"""
584-
old code, should work with all basis and filters. slow.
585-
"""
586-
basis_mats = self.basis.filter_stack_to_basis_mats(
587-
self.src.filter_stack,
588-
pixel_size=self.src.pixel_size,
589-
expand_method=self.expand_method,
590-
)
591-
592-
# from tqdm import trange
593-
# diff = 0
594-
# for i in trange(len(basis_mats)):
595-
# a = basis_mats[i]
596-
# ref= old_basis_mats[i]
597-
# for j in range(len(ref)):
598-
# diff += np.sum(a[j]-ref[j])
599-
600-
# print("sum of diff across all filters", diff)
601-
# breakpoint()
602-
603-
return basis_mats
580+
logger.info("Using basis.filter_stack_to_basis_mats.")
581+
return self.basis.filter_stack_to_basis_mats(
582+
self.src.filter_stack,
583+
pixel_size=self.src.pixel_size,
584+
expand_method=self.expand_method,
585+
)
604586

605587
def _radial_filter_stack_to_basis_mats(self):
606588
logger.info("Generating filter eval points")

0 commit comments

Comments
 (0)